0

My question is a little different from similar posts, in a sense that I don't have any other array to merge with.

I want to merge arrays with in multi-dimensional array.

So that it's not a multi dimensional any more.

Here is the array I have:

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
        )

)

What I am trying is merging all arrays with in. But the index (TotalMonths) is common so only for that it should sum values like (1+5+3) = 8 which will be reflected in new merged array.

I have tried this example also, but I am not sure how I am getting same values.

This is what I have tried so far:

print_r($collidingMonths);
$outPutArray = array();
foreach($collidingMonths as $innerArray) {
    $outPutArray[key($innerArray)] = current($innerArray);
}

print_r($outPutArray);

But I am getting a result that I don't want:

Array
(
    [TotalMonths] => 3
)
2
  • $outPutArray[key($innerArray)] = current($innerArray); why you are using current ? Commented Apr 28, 2016 at 5:24
  • what is your desired output look like??? I mean output array. Commented Apr 28, 2016 at 5:26

3 Answers 3

1

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);
}

=-=-==-=-=-=-

UPDATE:

Thanks, very nice work here. I had to do little changes. but all is your code. Providing My Updated Code so might be helpful for some one.

Updated Code

        $outPutMonths = [];
        $TotalMonths = 0;
        foreach($collidingMonths as $innerArray) {
            $TotalMonths += $innerArray['TotalMonths'];
            unset($innerArray['TotalMonths']);
            $outPutMonths = array_merge($outPutMonths, $innerArray);
        }
        $outPutMonths['TotalMonths'] = $TotalMonths;

Updated Result (Desired Result) :

Array
(
[TotalMonths] => 9
[0] => 2015-07-01
[1] => 2015-11-01
[2] => 2015-12-01
[3] => 2016-01-01
[4] => 2016-02-01
[5] => 2016-03-01
[6] => 2016-04-01
[7] => 2016-05-01
[8] => 2016-06-01
)
Sign up to request clarification or add additional context in comments.

Comments

1

I think the expected output OP wants is:

array
(
    [TotalMonths] => 9,
    [0] => 2015-07-01,
    [1] => 2015-11-01,
    [2] => 2015-12-01,
    [3] => 2016-01-01,
    [4] => 2016-02-01,
    [5] => 2016-03-01,
    [6] => 2016-04-01,
    [7] => 2016-05-01,
    [8] => 2016-06-01,
)

To get that, we can do something like this:

$outPutArray = array();

foreach($collidingMonths as $timestamp => $monthsArray)
{
    foreach($monthsArray as $key => $value)
    {
        if(is_numeric($value))
        {
            if(isset($outPutArray[$key]))
                $outPutArray[$key] += $value;
            else 
                $outPutArray[$key] = $value;
        }
        else
        {
            array_push($outPutArray, $value);
        }
}

1 Comment

Thankyou very much. Your answer is also great. I upvoted it. But i selected @Sergey answer as he was the first to answer and he has only 1 foreach loop so i guess its more efficent. But your code is working great too, which i have tested. Thanks.
0

Simply use the array_sum and array_column. Let $collidingMonths as main array.

$arr2 = array_column($collidingMonths, 'TotalMonths');
echo array_sum($arr2);

Result

Output Array:

Array
(
    [0] => 1
    [1] => 5
    [2] => 3
)

Sum:

8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.