Home > Mobile >  array group by date and show randomly
array group by date and show randomly

Time:02-18

I have an array in the example below I need to show 2 random data from each date to my members from here.

$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"],
];

In short, what I want to do is to show 2 pieces of data from each random date by grouping them in each loop.

When you look at it, there are 3 dates in the array.

total 3 date

How can I randomly get the output like below in 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
        )

)

CodePudding user response:

Changed the ids as in the original post they are mostly 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);

Output:

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
                )

        )

)

This solution provides a result array formed differently with the dates as array keys:

$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);

Output:

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
        )

)
  • Related