PHP 按日期和 ID 分组

时间:2021-06-17 05:06:57

标签: php arrays sorting

假设我有这个数组

 [
    {
                    "id": "1_8u48bxhw",
                    "patterns": [
                        "22/04/2021 08:40:54",
                        "22/04/2021 08:43:23"
                    ]
                },
                {
                    "id": "1_1ds3ygvn",
                    "patterns": [
                        "22/04/2021 08:37:45",
                        "23/04/2021 08:53:38",
                        "26/04/2021 02:30:42",
                        "26/04/2021 02:31:22",
                        "26/04/2021 02:32:02"
                    ]
                },
                {
                    "id": "1_5z9ywdxv",
                    "patterns": [
                        "22/04/2021 08:36:43",
                        "23/04/2021 08:52:18",
                        "23/04/2021 08:53:00",
                        "23/04/2021 08:53:45",
                        "23/04/2021 08:54:04",
                        "23/04/2021 08:57:23",
                        "23/04/2021 08:57:23",
                        "23/04/2021 08:58:31"
                    ]
                },
                {
                    "id": "1_uwiyjoda",
                    "patterns": [
                        "22/04/2021 08:41:02",
                        "22/04/2021 08:41:10"
                    ]
                },
                {
                    "id": "1_zs32zdjr",
                    "patterns": [
                        "22/04/2021 08:42:38",
                        "22/04/2021 08:42:44"
                    ]
                },
                {
                    "id": "1_pr1hvl3x",
                    "patterns": [
                        "22/04/2021 08:36:58",
                        "22/04/2021 08:38:04",
                        "22/04/2021 08:38:20",
                        "22/04/2021 08:42:11"
                    ]
                }
 ]

我尝试将日期与 ID 分组。

我的预期结果应该是:

[ {
    "id": "1_8u48bxhw",
    "dates": {
        "22/04/2021": [
          "22/04/2021 08:40:54",
          "22/04/2021 08:43:23"
        ]
     }
   },
   {
     "id": "1_1ds3ygvn",
     "dates": {
           "22/04/2021": [
               "22/04/2021 08:37:45"
            ],
            "23/04/2021": [
                "23/04/2021 08:53:38"
            ],
            "26/04/2021": [
                 "26/04/2021 02:30:42",
                 "26/04/2021 02:31:22",
                 "26/04/2021 02:32:02"
            ]
      }
    },
    // and so on...
 ]

这是我试过的代码

    $groupDate = array();
    $groupByDate = [];
    $watchPatterns = [];
    foreach ($watchDatetime as $watchTime) {
        $id = $watchTime['id'];
        $patterns = $watchTime['patterns'];
        $dateGrouped = null;
        if ($patterns) {
            foreach ($patterns as $pattern) {
                $dateSplit = explode(" ", $pattern);
                $date = $dateSplit[0];
                $groupDate[$date][] = $pattern;
            }
        }
        array_push($groupByDate, [
            'id' => $id,
            'dates' => $groupDate
        ]);
    }
    array_push($watchPatterns, $groupByDate);

但结果并不像我预期的那样。 以下是实际结果:

{
                    "id": "1_8u48bxhw",
                    "dates": {
                        "22/04/2021": [
                            "22/04/2021 08:40:54",
                            "22/04/2021 08:43:23"
                        ]
                    }
                },
                {
                    "id": "1_1ds3ygvn",
                    "dates": {
                        "22/04/2021": [
                            // duplicate from first
                            "22/04/2021 08:40:54",
                            "22/04/2021 08:43:23",
                             //
                            "22/04/2021 08:37:45"
                        ],
                        "23/04/2021": [
                            "23/04/2021 08:53:38"
                        ],
                        "26/04/2021": [
                            "26/04/2021 02:30:42",
                            "26/04/2021 02:31:22",
                            "26/04/2021 02:32:02"
                        ]
                    }
                },

如果您注意到第二个数组不断复制第一个数组中的日期,对数组的其余部分依此类推。

1 个答案:

答案 0 :(得分:0)

就像 Tobias K. 先生所说的那样。您需要为每次迭代重置数组

// Accept an external ArrayBuffer from a service (cloud,external server etc.)
    var request = new XMLHttpRequest();

// For testing lets say this is your URL
request.open( "GET", "https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Circle-icons-computer.svg/512px-Circle-icons-computer.svg.png", true );

// Identify that your response has declared as arraybuffer
request.responseType = "arraybuffer";

// If you are using DOM object, definitelly you will use blob to create your file object 
request.onload = function( e ) {
   
    var arrayBufferView = new Uint8Array( this.response );
    var blob = new Blob( [ arrayBufferView ], { type: "image/jpeg" } );
  
    // Parse and create your image from url
    var urlCreator = window.URL || window.webkitURL;
    var imageUrl = urlCreator.createObjectURL( blob );
    
    // Access your div element, where your photo will be presented.
    // Here instead you can alter the code to download the image as file...
    var img = document.querySelector( "#myImage" );
    img.src = imageUrl;
};

request.send();