Search code examples
phparraysmultidimensional-arraymergegrouping

Merge and group rows from multiple 2d arrays based on a shared column value then push another column's data into subarrays


Scenario:

I have these 2 arrays:

array1:

Array
(
[1] => Array
    (
        [label] => pending
        [fillColor] => #468847
        [data] => 50
    )

[2] => Array
    (
        [label] => dispatched
        [fillColor] => #6ecf70
        [data] => 10
    )

[3] => Array
    (
        [label] => delivered
        [fillColor] => #f89406
        [data] => 1
    )

[4] => Array
    (
        [label] => invoiced
        [fillColor] => #3a87ad
        [data] => 2
    )

)

array2:

Array
(
[1] => Array
    (
        [label] => pending
        [fillColor] => #468847
        [data] => Array
            (
                [0] => 1
            )

    )

)

The result I need is

Array
(
[1] => Array
    (
        [label] => pending
        [fillColor] => #468847
        [data] => Array
            (
                [0] => 50
                [1] => 1
            )
    )

[2] => Array
    (
        [label] => dispatched
        [fillColor] => #6ecf70
        [data] => Array
            (
                [0] => 10
                [1] => 0
            )
    )

[3] => Array
    (
        [label] => delivered
        [fillColor] => #f89406
        [data] => Array
            (
                [0] => 1
                [1] => 0
            )
    )

[4] => Array
    (
        [label] => invoiced
        [fillColor] => #3a87ad
        [data] => Array
            (
                [0] => 2
                [1] => 0
            )
    )

)

There are only 4 labels:

  • pending
  • dispatched
  • delivered
  • invoiced

Please note that the arrays are just an example. It can happen that the first array has no values at all or just 2 and the second array have 3 values or none.

Because of that constraint above I'm thinking to use array_replace and having an array called

base_array = ["pending", "dispatched", "delivered", "invoiced"]

I have tried to loop the base_array and try to match the array1 with array2 if label exist.

Basically, if key (which is label) is not exist in any of array1 or array2 then the value replaced will be 0 in the resulting array.

I have tried

foreach($base_array as $key => $value) {
    if(in_array($key, $array1[$key])) {
        $array[$key] = $array1[$key];
    }
}

but it looks like I'm lost on these multi dimensional arrays and replacing. Any help will be really appreciated. Thanks.


Solution

  • From what i understand from your question you can do it like this :-

        $array = array(
        '1' => Array
            (
            'label' => 'pending',
            'fillColor' => '#468847',
            'data' => '50'
        ),
        '2' => Array
            (
            'label' => 'dispatched',
            'fillColor' => '#6ecf70',
            'data' => '10'
        ),
        '3 ' => Array
            (
            'label' => 'delivered',
            'fillColor' => '#f89406',
            'data' => '1'
        ),
        '4' => Array
            (
            'label' => 'invoiced',
            'fillColor' => '#3a87ad',
            'data' => '2'
        ),
    );
    
    $array2 = array
        (
        '1' => Array
            (
            'label' => 'pending',
            'fillColor' => '#468847',
            'data' => array
                (
                '0' => '1'
            )
        )
    );
    
    $temp = array();
    $i = 0;
    foreach ($array as $key => $value) {
    
        $temp[$key]['label'] = $value['label'];
        $temp[$key]['fillColor'] = $value['fillColor'];
    
        foreach ($array2 as $key2 => $value2) {
    
            if ($value['fillColor'] == $value2['fillColor'] && $value['label'] == $value2['label']) {
                $temp[$key]['data'][] = $value['data'];
                if (isset($value2['data'][$i])) {
                    $temp[$key]['data'][] = $value2['data'][$i];
                }
            } else {
                $temp[$key]['data'][] = $value['data'];
                if (!isset($value2['data'][$i])) {
                    $temp[$key]['data'][] = 0;
                }
            }
            $i++;
        }
    }
    
    echo '<pre>';
    print_r($temp);