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.

share|improve this question
    
can you explain your result array data key ? what will be your expected result ? – rocky Nov 2 '15 at 16:48
    
The expected array result is above. Thanks. – Adrian P. Nov 2 '15 at 16:50
    
Thank you for your time. I have rewrite the SQL query and get better array in return. – Adrian P. Nov 2 '15 at 17:34
up vote 1 down vote accepted

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);
share|improve this answer
    
It is working for the arrays I gave but not the general case. The print_r($temp) return Array ( [1] => Array ( [label] => pending [fillColor] => #468847 [data] => Array ( [0] => 50 [1] => 1 ) ) [2] => Array ( [label] => dispatched [fillColor] => #6ecf70 ) [3 ] => Array ( [label] => delivered [fillColor] => #f89406 ) [4] => Array ( [label] => invoiced [fillColor] => #3a87ad ) ) – Adrian P. Nov 2 '15 at 17:22
    
please explain your expected result so that i can help more – rocky Nov 2 '15 at 17:23
    
The expected temp array is to have "data" as an array with 0 if not found. Thank you for your time. I have rewrite the SQL query and get better array in return. – Adrian P. Nov 2 '15 at 17:33
    
check the edit post – rocky Nov 2 '15 at 17:37
    
That's exactly what I need. Thanks. I will accept your answer. – Adrian P. Nov 2 '15 at 17:41

Try this out:

$array1 = array(
    array(
        'label'     => 'pending',
        'fillColor' => '#468847',
        'data'      => '50'
    ),
    array(
        'label'     => 'dispatched',
        'fillColor' => '#468847',
        'data'      => '10'
    ),
    array(
        'label'     => 'delivered',
        'fillColor' => '#468847',
        'data'      => '8'
    ),
    array(
        'label'     => 'invoiced',
        'fillColor' => '#468847',
        'data'      => '5'
    )

);
$array2 = array(
    array(
        'label'     => 'pending',
        'fillColor' => '#468847',
        'data'      => array()
    ),
    array(
        'label'     => 'dispatched',
        'fillColor' => '#6ecf70',
        'data'      => array()
    ),
    array(
        'label'     => 'delivered',
        'fillColor' => '#f89406',
        'data'      => array()
    ),
    array(
        'label'     => 'invoiced',
        'fillColor' => '#3a87ad',
        'data'      => array()
    )
);

foreach ($array1 as $order) {
    foreach ($array2 as $key => $group) {
        if ($order['label'] == $group['label']) {
            array_push($array2[$key]['data'], $order['data']);
        }
    }
}

var_dump($array2);
share|improve this answer
    
Thank you for your time. I have rewrite the SQL query and get better array in return. BTW it is not working. – Adrian P. Nov 2 '15 at 17:34
    
The code works I tested it multiple times ideone.com/YyAZKP – Fluinc Nov 2 '15 at 19:15
    
It works but not give the expected result. See answer above. – Adrian P. Nov 2 '15 at 19:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.