I asked a question several days ago that was answered well, but now I've implemented the solution and facing a few issues. (Thanks to Oleg for getting this far)
I'm trying to merge and sum the following arrays:
Array
(
[Thursday] => Array
(
[Lunch] => Array
(
[Total] => 10
)
[Date] => 2015-12-31
)
)
Array
(
[Thursday] => Array
(
[Lunch] => Array
(
[Guarantees] => 231
)
[Date] => 2015-12-31
)
)
Array
(
[Friday] => Array
(
[Breakfast] => Array
(
[Total] => 1
)
[Date] => 2016-01-01
[Lunch] => Array
(
[Total] => 1
)
)
[Thursday] => Array
(
[Lunch] => Array
(
[Total] => 1
)
[Date] => 2015-12-31
)
)
And I'm using the following code:
private function _merge( $arr1, $arr2, $arr3 )
{
$maxArraysCount = func_num_args();
$return = array();
for( $i = 1; $i < $maxArraysCount; $i++ ){
$arr = 'arr' . $i;
if( isset( $$arr ) && is_array( $$arr )){
foreach ( $$arr as $day => $value ) {
foreach ( $value as $meal => $time ) {
if( $meal == "Date"){
$return[$day][$meal] = $time;
} else {
foreach ( $time as $type => $count ) {
if( !isset( $return[$day][$meal][$type] ) )
$return[$day][$meal][$type] = 0;
$return[$day][$meal][$type] = $count + $return[$day][$meal][$type];
}
}
}
}
}
}
return $return;
}
And it's outputting this:
Array
(
[Thursday] => Array
(
[Lunch] => Array
(
[Total] => 11
)
[Date] => 2015-12-31
)
[Friday] => Array
(
[Breakfast] => Array
(
[Total] => 1
)
[Date] => 2016-01-01
[Lunch] => Array
(
[Total] => 1
)
)
)
There is missing information in this merged/summed array. Notice there are no "Guarantees" at all. It appears that arr1 has a higher priority than arr3?