1

I have an array like that:

array(
    [0] => array(
        'date' => '2017-12-20',
        'name' => 'test A',
        'kwa' => 'kwa1, kwa2'
    ),
    [1] => array(
        'date' => '2017-12-20',
        'name' => 'test B',
        'kwa' => ''
    ),
    [2] => array(
        'date' => '2017-12-21',
        'name' => 'test C',
        'kwa' => 'kwa1'
    ),
    [3] => array(
        'date' => '2017-12-22',
        'name' => 'test D',
        'kwa' => ''
    ),
    [4] => array(
        'date' => '2017-12-22',
        'name' => 'test E',
        'kwa' => ''
    ),
)

I want group all elements has the same day into a array. Something like that:

array(
    ['2017-12-20'] => array( '0', '1'),
    ['2017-12-21'] => array( '2' ),
    ['2017-12-22'] => array( '3', '4' ),
)

PHP has any function to do this? Or anyone know the best solution? Thank you!!!

Mít
  • 195
  • 1
  • 3
  • 18
  • 3
    an easy solution is to iterate over your main array, and as you encounter dates build a new array and insert into the correct place. Hope this helps! – N. Ivanov Dec 21 '17 at 09:55
  • 2
    Possible duplicate of [PHP create array group by key](https://stackoverflow.com/questions/45965029/php-create-array-group-by-key) – Roy Bogado Dec 21 '17 at 09:56
  • @N.Ivanov I already think about your idea, but still find other maybe better than? :D – Mít Dec 21 '17 at 09:56

3 Answers3

2

Here is your solution...

Input

<?php 
    $array = array(
        array(
            'date' => '2017-12-20',
            'name' => 'test A',
            'kwa' => 'kwa1, kwa2'
        ),array(
            'date' => '2017-12-20',
            'name' => 'test B',
            'kwa' => ''
        ),array(
            'date' => '2017-12-21',
            'name' => 'test C',
            'kwa' => 'kwa1'
        ),array(
            'date' => '2017-12-22',
            'name' => 'test D',
            'kwa' => ''
        ),array(
            'date' => '2017-12-22',
            'name' => 'test E',
            'kwa' => ''
        ),
    );

Solution

    $new = array();
    foreach($array as $r){
       $new[$r['date']][] = array('name'=>$r['name'],'kwa' => $r['kwa']); 
    }
    echo "<pre>";print_r($new);

?>

Output

    Array
(
    [2017-12-20] => Array
        (
            [0] => Array
                (
                    [name] => test A
                    [kwa] => kwa1, kwa2
                )

            [1] => Array
                (
                    [name] => test B
                    [kwa] => 
                )

        )

    [2017-12-21] => Array
        (
            [0] => Array
                (
                    [name] => test C
                    [kwa] => kwa1
                )

        )

    [2017-12-22] => Array
        (
            [0] => Array
                (
                    [name] => test D
                    [kwa] => 
                )

            [1] => Array
                (
                    [name] => test E
                    [kwa] => 
                )

        )

)
GYaN
  • 2,327
  • 4
  • 19
  • 39
2
$arr = array();

foreach($array1 as $key => $item)
{
   $arr[$item['date']][$key] = $key;
}

echo "<pre>";
print_r($arr);
TarangP
  • 2,711
  • 5
  • 20
  • 41
1

I would use something like the following:

$array = [
    [
        'date' => '2017-12-20',
        'name' => 'test A',
        'kwa' => 'kwa1, kwa2'
    ],
    [
        'date' => '2017-12-20',
        'name' => 'test B',
        'kwa' => ''
    ],
    [
        'date' => '2017-12-21',
        'name' => 'test C',
        'kwa' => 'kwa1'
    ],
    [
        'date' => '2017-12-22',
        'name' => 'test D',
        'kwa' => ''
    ],
    [
        'date' => '2017-12-22',
        'name' => 'test E',
        'kwa' => ''
   ],
];

$formatted = [];
foreach ($array as $k => $v) {
    $formatted[$v['date']] = $formatted[$v['date']] ?? [];
    array_push($formatted[$v['date']], $k);
}

print_r($formatted);
Tom Wright
  • 2,841
  • 4
  • 22
  • 30