Mít
Mít

Reputation: 303

Group 2d array data by a column and push the original row's index into the group's subarray

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' ),
)

Upvotes: 1

Views: 67

Answers (4)

mickmackusa
mickmackusa

Reputation: 48071

Because PHP's foreach() assigns values before keys in each iteration, a body-less loop can be implemented.

Array destructuring syntax is used to declare a variable for the date column value, then the key is pushed as a new value into the result's date-specific subarray.

Code: (Demo)

$result = [];
foreach ($array as $result[$d][] => ['date' => $d]);
var_export($result);

Output:

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

Relevant reading: Within a foreach() expression, is the value defined before or after the key?

Upvotes: 0

Tom Wright
Tom Wright

Reputation: 2861

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

Upvotes: 1

TarangP
TarangP

Reputation: 2738

$arr = array();

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

echo "<pre>";
print_r($arr);

Upvotes: 2

Gyandeep Sharma
Gyandeep Sharma

Reputation: 2327

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] => 
                )

        )

)

Upvotes: 2

Related Questions