根据用户和创建时间对php数组项进行分组
这是显示用户上传照片的对象数组:
Array
(
[12] => stdClass Object
(
[type] => photo
[created] => 2010-05-14 23:36:41
[user] => stdClass Object
(
[id] => 760
[username] => mrsmith
)
[photo] => stdClass Object
(
[id] => 4181
)
)
[44] => stdClass Object
(
[type] => photo
[created] => 2010-05-14 23:37:15
[user] => stdClass Object
(
[id] => 760
[username] => mrsmith
)
[photo] => stdClass Object
(
[id] => 4180
)
)
)
但是,不是显示:
史密斯先生上传了一张照片
史密斯先生上传了一张照片
照片我想显示的
- :史密斯先生上传了两张照片
,方法是对相似的项目进行分组,按用户 ID 分组,然后将它们添加到相隔 15 分钟内。所以我想获得这种形状的数组:
Array
(
[12] => stdClass Object
(
[type] => photo
[created] => 2010-05-14 23:36:41
[user] => stdClass Object
(
[id] => 760
[username] => mrsmith
)
[photos] => Array
(
[0] => stdClass Object
(
[id] => 4181
)
[1] => stdClass Object
(
[id] => 4180
)
)
)
)
保留组的第一个项目及其创建时间,并用任何其他可分组照片对其进行补充,然后取消设置分组的任何项目(因此最终的数组不会)不再有密钥 44,因为它与 12 分组)。
该数组不仅包含照片,还包含其他操作,因此原始键为 12 和 44。我只是想不出一种有效的方法。我曾经使用 MySQL 和 PHP 来执行此操作,但出于缓存原因,我尝试仅使用纯 PHP。
任何人都可以提供任何见解吗?我考虑过检查每一项,看看是否可以将其与数组中的前一项分组,但前一项可能不一定相关,甚至不一定是照片。我的大脑完全冻结了:(
This is an array of objects showing a user uploading photos:
Array
(
[12] => stdClass Object
(
[type] => photo
[created] => 2010-05-14 23:36:41
[user] => stdClass Object
(
[id] => 760
[username] => mrsmith
)
[photo] => stdClass Object
(
[id] => 4181
)
)
[44] => stdClass Object
(
[type] => photo
[created] => 2010-05-14 23:37:15
[user] => stdClass Object
(
[id] => 760
[username] => mrsmith
)
[photo] => stdClass Object
(
[id] => 4180
)
)
)
However instead of showing:
mr smith uploaded one photo
mr smith uploaded one photo
I'd like to display:
- mr smith uploaded two photos
by grouping similar items, grouping by user ID and them having added them within, let's say 15 minutes of each other. So I'd like to get the array in this sort of shape:
Array
(
[12] => stdClass Object
(
[type] => photo
[created] => 2010-05-14 23:36:41
[user] => stdClass Object
(
[id] => 760
[username] => mrsmith
)
[photos] => Array
(
[0] => stdClass Object
(
[id] => 4181
)
[1] => stdClass Object
(
[id] => 4180
)
)
)
)
preserving the first item of the group and it's created time, and supplementing it with any other groupable photos and then unsetting any items that were grouped (so the final array doesn't have key 44 anymore as it was grouped in with 12).
The array contains other actions than just photos, hence the original keys of 12 and 44. I just can't figure out a way to do this efficiently. I used to use MySQL and PHP to do this but am trying to just use pure PHP for caching reasons.
Can anyone shed any insights? I thought about going through each item and seeing if I can group it with the previous one in the array but the previous one might not necessarily be relevant or even a photo. I've got total brain freeze :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果条目按日期排序,您可以执行以下操作:
$foo
),其中包含 15 分钟窗口中的所有照片。使用数组作为 FIFO 列表(使用array_shift
删除项目并< code>$foo[] = ... 添加)。$foo
开头删除所有日期比当前项目早 15 分钟以上的条目。一旦您找到一个与 15 分钟内发生的事情相关的项目,您就可以停止(这是一个单行 for 循环)。$foo
中的条目$foo
中的引用,以便添加新数据$foo = &$var
将当前项的引用添加到$foo
末尾。然后,您可以使用 array_values 重新索引原始数组以填补空白。
If the entries are ordered by date you can do the following:
$foo
) with all the photos in a 15 minute window. Use the array as a FIFO list (usearray_shift
to remove items and$foo[] = ...
to add).$foo
all the entries whose date is more than 15 minutes less than the current item. Once you find an item that for something that happened less than 15 minutes ago, you can stop (it's a one line for loop).$foo
$foo
so that the new data is added$foo
with$foo = &$var
.You may then reindex the original array with array_values to fill the gaps.