Sizzling Code
Sizzling Code

Reputation: 6070

Merge multi dimensional PHP array with in

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
)

Upvotes: 0

Views: 55

Answers (3)

Sergey Mitroshin
Sergey Mitroshin

Reputation: 113

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
)

Upvotes: 1

npenm
npenm

Reputation: 36

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

Upvotes: 1

Murad Hasan
Murad Hasan

Reputation: 9583

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

Upvotes: 0

Related Questions