this kinda question might have been asked many times, but mine is little difference in a sense that i dont have any other array to merge with.
i want to merge arrays with in multidimentional array.
So that its not a multi dimentional any more.
Here is the array i have got.
Array
(
[2013-12-01::2015-07-29] => Array
(
[TotalMonths] => 1
[0] => 2015-07-01
)
[2015-11-01::2016-03-30] => Array
(
[TotalMonths] => 5
[0] => 2015-11-01
[1] => 2015-12-01
[2] => 2016-01-01
[3] => 2016-02-01
[4] => 2016-03-01
)
[2016-04-01::2017-11-30] => Array
(
[TotalMonths] => 3
[0] => 2016-04-01
[1] => 2016-05-01
[2] => 2016-06-01
)
)
(1+5+3) = 8
print_r($collidingMonths);
$outPutArray = array();
foreach($collidingMonths as $innerArray) {
$outPutArray[key($innerArray)] = current($innerArray);
}
print_r($outPutArray);
Array
(
[TotalMonths] => 3
)
The most straight-forward way would be to take the TotalMonths
value out of the array elements before merging:
$result = [];
$totalMonths = 0;
foreach($collidingMonths as $innerArray) {
$TotalMonths += $innerArray['TotalMonths'];
unset($innerArray['TotalMonths']);
$result = array_merge($result, $innerArray);
}