-2

Help me pls combine array values. I have the same array: In my case i should save customers name like a key.

    Array
(
    [Test Name] => Array
        (
            [0] => Array
                (
                   [name] => 
                  banana
                    [id] => 
                  45002
                    [quantity] => 
                  10
                )
            [1] => Array
                (
                    [name] => 
                 banana
                    [id] => 
                 45002
                    [quantity] => 
                   20
                )
            [3] => Array
                (
                    [name] => 
                    apple 
                    [id] => 
                   23402
                    [qua] => 
                    1
                )

            [5] => Array
                (
                    [name] => 
                   cherry 
                    [id] => 
                   40017
                    [qua] => 
                   7
                )

How to get something like this:

Array
(
    [Test Name] => Array
        (
            [0] => Array
                (
                   [name] => 
                  banana
                    [id] => 
                  45002
                    [quantity] => 
                  30 // summ quantity but unique name and id 
                )
            [1] => Array
                (
                    [name] => 
                    apple 
                    [id] => 
                   23402
                    [qua] => 
                    1
                )

            [2] => Array
                (
                    [name] => 
                   cherry 
                    [id] => 
                   40017
                    [qua] => 
                   7
                )

In my case i should save customers name like a key. Then i will upload this on a table.

2
  • See here Commented May 5, 2016 at 10:47
  • Hi. You need quantity & qua as different fields or same? Commented May 5, 2016 at 10:59

1 Answer 1

0

You can use array_map and array_reduce to accomplish it:

$results = array_map(function ($result) {
    return array_reduce($result, function ($carry, $item) {
        if (isset($carry[$item['id']])) {
            $carry[$item['id']]['quantity'] += $item['quantity'];
        } else {
            $carry[$item['id']] = $item;
        }
        return $carry;
    }, array());
}, $results);

From the documentation:

array_map() returns an array containing all the elements of array1 after applying the callback function to each one.

array_reduce() applies iteratively the callback function to the elements of the array, so as to reduce the array to a single value.

In our case the callback given to the array_reduce function returns a final array which contains unique items by id.

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

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.