Jason Whalan
Jason Whalan

Reputation: 17

Loop through a multidimensional array and add values to new array while handling elements with some matching values differently

I have the following array

Array (
    [0] => Array (
        [0] => Array (
            [productCode] => HQ768H
            [startTimeLocal] => 2018-04-17 14:00:00
            [endTimeLocal] => 2018-04-17 16:00:00
            [totalQuantity] => 2
            [amount] => 170
            [extras] => Array ()
            [transferReturn] =>
            [subtotal] => 170
        )
        [1] => Array (
            [productCode] => PLJ6HP
            [startTimeLocal] => 2018-04-15 14:00:00
            [endTimeLocal] => 2018-04-15 16:00:00
            [totalQuantity] => 2
            [amount] => 170
            [extras] => Array ()
            [transferReturn] =>
            [subtotal] => 170
        )
        [2] => Array (
            [productCode] => PLJ6HP
            [startTimeLocal] => 2018-04-15 14:00:00
            [endTimeLocal] => 2018-04-15 16:00:00
            [totalQuantity] => 2
            [amount] => 170
            [extras] => Array ()
            [transferReturn] =>
            [subtotal] => 170
        )
    )
)

I am trying to create another array from this using PHP made up of the productCode, startTimeLocal and totalQuantity... but where two or more elements have the same productCode and the same startTimeLocal I need to update the totalQuantity by adding the new value rather then adding a new array element.

The only code I have been able to come up with is a long set of nested foreach loops which don't come anywhere close to performing the actions I need. I am reasonably new to this. If anybody could help out or point me in the right direction I would be very appreciative. Thanks.

Below is what I am trying to achieve...

Array (
    [0] => Array (
        [0] => Array (
            [productCode] => HQ768H
            [startTimeLocal] => 2018-04-17 14:00:00
            [totalQuantity] => 2
        )
        [1] => Array (
            [productCode] => PLJ6HP
            [startTimeLocal] => 2018-04-15 14:00:00
            [totalQuantity] => 4
        )
    )
)

Upvotes: 0

Views: 854

Answers (1)

wayneOS
wayneOS

Reputation: 1425

You can do this by looping through your old array. then you add a new array as element if it not already exists. Otherwise you just add totalQuantity to your existing element.

$new_array = [];

foreach ($array as $inner_array)
    foreach ($inner_array as $element) {

        //check if already in $new_array and get index
        $index = -1;

        for ($i = 0; $i < count ($new_array); $i++)
            if ($new_array[$i]['productCode'] == $element['productCode'])
                if ($new_array[$i]['startTimeLocal'] == $element['startTimeLocal'])
                    $index = $i;

        //check if it was found
        if ($index == -1) {
            //if not add to $new_array
            $new_array[] = [
                'productCode' => $element['productCode'],
                'startTimeLocal' => $element['startTimeLocal'],
                'totalQuantity' => $element['totalQuantity']
            ];
        } else {
            //otherwise increase totalQuantity
            $new_array[$index]['totalQuantity'] += $element['totalQuantity']
        }

    }

Upvotes: 1

Related Questions