我想为社区站点存储和检索用户通知数据。当有人评论用户的帖子或有人关注用户时,应生成通知。我希望这种行为能够复制 Facebook 通知。所以它应该是这样的:
User A, User B and 3 other started following you.
User A, User Z and 2 others commented on your post Super Duper.
如果我创建一个通用数组来保存通知,那么它将如下所示:
$notification = array ($notification_id, notification_time, $read_status, $notification_recipient_id, $notification_initiator_id, $post_id=NULL, $comment_id=NULL); // This array represents a single notification
$notifications = array ($notification, ...); // This array is made up of many single notification array.
因此,如果我要从 $notifications 数组(包含所有单个通知数组的数组)中检索数据,我将使用 for 循环并在根据时间对数组进行排序后回显结果。这会给我这样的结果:
User A started following you.
User B started following you.
User C started following you.
User D started following you.
User E started following you.
User A commented on your post Super Duper.
User B commented on your post Super Duper.
User C commented on your post Super Duper.
User D commented on your post Super Duper.
因此,如果您查看我打算实现的结果以及我将从目前的阵列设计中获得的结果,两者都不同。我可以通过对 $notifications 数组执行 N 次可能涉及展平、递归 for 循环、排序等的操作来实现所需的结果。但是,我认为我可以通过重新设计数据在数组中的存储方式来减少开销,这样当需要检索数据时,我可以执行最少数量的操作。此外,由于在显示数据时需要考虑读取状态,因此要达到我想要的结果会变得更加复杂。我请求有关设计数组结构的建议以及如何从数组中检索数据以实现我想要的结果的示例。