I have
Array_A ( [0] => Array ( [DATE] => 2012-11-28 , [totalCount] => )
[1] => Array ( [DATE] => 2012-11-29 , [totalCount] => )
[2] => Array ( [DATE] => 2012-11-30 , [totalCount] => ) )
Array_B ( [10] => Array ( [DATE] => 2012-11-28 , [totalCount] => 30 )
[11] => Array ( [DATE] => 2012-11-30 , [totalCount] => 40 )
[12] => Array ( [DATE] => 2012-12-05 , [totalCount] => 50 ) )
How to I do if I need to replace the Array_A with the values from the Array_B and the output should be
Array_A ( [0] => Array ( [DATE] => 2012-11-28 , [totalCount] => 30 )
[1] => Array ( [DATE] => 2012-11-29 , [totalCount] => )
[2] => Array ( [DATE] => 2012-11-30 , [totalCount] => 40 ) )
I just noticed the part where you only want the keys from array_a
to be used, this loop wil do.
foreach($array_a as $key => $value){
if (array_key_exists($key, $array_b){
$result[$key] = $array_b[$key];
} else {
$result[$key] = null;
}
}
Or, if you want the value of array_a
being used if the key in array_b
does not exist, simply replace the statement in the else
-clause to: $result[$key] = $value;
.
Edit (because of the comment of mickmackusa)
I think I misread the question back in the day and was expecting the key of the second array to be unique (like an identiefier in a table). So I was expecting array_b to be something like:
$array_b = [
0 => ['DATE' => '2012-11-28', 'totalCount' => 30],
2 => ['DATE' => '2012-11-30', 'totalCount' => 40],
12 => ['DATE' => '2012-12-05', 'totalCount' => 50],
];
In that case the earlier mentioned code will work. Demo
See the answer from mickmackusa as that actually solves the issue of the OP.