按日期分组并随机显示

array group by date and show randomly

我在下面的例子中有一个数组 我需要从这里向我的成员显示每个日期的 2 个随机数据。

$array =[
    0 =>["id" => "1020","date" => "Friday March 15"],
    1 =>["id" => "1028","date" => "Friday March 15"],
    2 =>["id" => "1023","date" => "Friday March 15"],
    3 =>["id" => "1028","date" => "Friday March 15"],
    4 =>["id" => "1028","date" => "Friday March 16"],
    5 =>["id" => "1028","date" => "Friday March 16"],
    6 =>["id" => "1028","date" => "Friday March 16"],
    7 =>["id" => "1028","date" => "Friday March 17"],
    8 =>["id" => "1028","date" => "Friday March 17"],
    9 =>["id" => "1028","date" => "Friday March 17"],
    10 =>["id" => "1028","date" => "Friday March 17"],
];

简而言之,我想做的是通过在每个循环中对它们进行分组来显示每个随机日期的 2 条数据。

查看时,数组中有 3 个日期。

共 3 个日期

如何在 foreach 中随机获得如下所示的输出

Array
(
    [0] => Array
        (
            [id] => 1028
            [date] => Friday March 15
        )

    [1] => Array
        (
            [id] => 1023
            [date] => Friday March 15
        )


)
Array
(
    [0] => Array
        (
            [id] => 1022
            [date] => Friday March 16
        )

    [1] => Array
        (
            [id] => 1026
            [date] => Friday March 16
        )

)
Array
(
    [0] => Array
        (
            [id] => 1016
            [date] => Friday March 17
        )

    [1] => Array
        (
            [id] => 1011
            [date] => Friday March 17
        )

)

更改了原来的 ID post 它们大多是 1028:

$array = [
    [ 'id' => '1020', 'date' => 'Friday March 15' ],
    [ 'id' => '1028', 'date' => 'Friday March 15' ],
    [ 'id' => '1023', 'date' => 'Friday March 15' ],
    [ 'id' => '1021', 'date' => 'Friday March 15' ],
    [ 'id' => '1023', 'date' => 'Friday March 16' ],
    [ 'id' => '1028', 'date' => 'Friday March 16' ],
    [ 'id' => '1022', 'date' => 'Friday March 16' ],
    [ 'id' => '1028', 'date' => 'Friday March 17' ],
    [ 'id' => '1022', 'date' => 'Friday March 17' ],
    [ 'id' => '1021', 'date' => 'Friday March 17' ],
    [ 'id' => '1023', 'date' => 'Friday March 17' ],
];

$dates = array_unique(array_column($array, 'date'));

$result = [];
foreach ($dates as $date) {
  $items = array_filter($array, fn($value) => $value['date'] === $date);
  shuffle($items);
  $result[] = [ $items[0], $items[1] ];
}

print_r($result);

输出:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 1023
                    [date] => Friday March 15
                )

            [1] => Array
                (
                    [id] => 1020
                    [date] => Friday March 15
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 1028
                    [date] => Friday March 16
                )

            [1] => Array
                (
                    [id] => 1023
                    [date] => Friday March 16
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 1022
                    [date] => Friday March 17
                )

            [1] => Array
                (
                    [id] => 1021
                    [date] => Friday March 17
                )

        )

)

此解决方案提供了一个以日期作为数组键的不同格式的结果数组:

$array = [
    [ 'id' => '1020', 'date' => 'Friday March 15' ],
    [ 'id' => '1028', 'date' => 'Friday March 15' ],
    [ 'id' => '1023', 'date' => 'Friday March 15' ],
    [ 'id' => '1021', 'date' => 'Friday March 15' ],
    [ 'id' => '1023', 'date' => 'Friday March 16' ],
    [ 'id' => '1028', 'date' => 'Friday March 16' ],
    [ 'id' => '1022', 'date' => 'Friday March 16' ],
    [ 'id' => '1028', 'date' => 'Friday March 17' ],
    [ 'id' => '1022', 'date' => 'Friday March 17' ],
    [ 'id' => '1021', 'date' => 'Friday March 17' ],
    [ 'id' => '1023', 'date' => 'Friday March 17' ],
];

$result = array_flip(array_unique(array_column($array, 'date')));

array_walk($result, function (&$item, $date) use ($array) {
  $items = array_filter($array, fn($value) => $value['date'] === $date);
  shuffle($items);
  $item = [ $items[0]['id'], $items[1]['id'] ];
}, array_keys($result));

print_r($result);

输出:

Array
(
    [Friday March 15] => Array
        (
            [0] => 1021
            [1] => 1023
        )

    [Friday March 16] => Array
        (
            [0] => 1023
            [1] => 1028
        )

    [Friday March 17] => Array
        (
            [0] => 1022
            [1] => 1021
        )

)