0

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
        )
    )
)
1
  • if you want an answer first please accept the edit someone did for your post... because in its current form its unreadable, and can you also make an example array of how you want the result to look like? Commented Apr 14, 2018 at 13:46

1 Answer 1

1

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']
        }

    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I think this is along the lines I needed. Currently I get the following result though and not really sure why... Array ( [] => Array ( [startTimeLocal] => [totalQuantity] => 0 ) [v] => Array ( [startTimeLocal] => v [totalQuantity] => v ) ) Also, I think this may only check for productCode matches... how would I go about checking that the productCode and the startTimeLocal matches(both must match)?
@JasonWhalan sorry i didn't saw that. i updated my answer to fit your needs. Hope it helps.
People like you are keeping the internet turning. Thanks so much, I really appreciate this, I spent days trying to do this before coming here. Thanks again.

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.