根据用户和创建时间对php数组项进行分组

发布于 2024-09-01 12:06:02 字数 2030 浏览 5 评论 0原文

这是显示用户上传照片的对象数组:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2024-09-08 12:06:02

如果条目按日期排序,您可以执行以下操作:

  • 保留一个数组(我们称之为 $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:

  • Keep an array (let's call it $foo) with all the photos in a 15 minute window. Use the array as a FIFO list (use array_shift to remove items and $foo[] = ... to add).
  • Iterate over the original array
    • Check the date of the current item and remove from the beginning of $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).
    • Iterate over the the entries in $foo
    • If a photo by the same person is found, then
      • Alter the reference stored in $foo so that the new data is added
      • Remove the current item from the original array
      • break from inner loop
    • If not
      • Add a reference to the current item to the end $foo with $foo = &$var.

You may then reindex the original array with array_values to fill the gaps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文