Sum of an array and concatenation of PHP value

0

I have a dynamic array, it contains keys that duplicate (TAG):

[0] => Array
        (
            [0] => Array
                (
                    [tag]        => 5x1 Acessori Kids
                    [pedido]     => 6701622409
                    [quantidade] => 2125
                )

        )

    [1] => Array
        (
            [1] => Array
                (
                    [tag]        => 5x1 Acessori Kids
                    [pedido]     => 6701622422
                    [quantidade] => 3705
                )

        )

    [2] => Array
        (
            [2] => Array
                (
                    [tag]        => RIABASIC5- 5X1 - 2 COMPOSIÇOES
                    [pedido]     => 6701622411
                    [quantidade] => 3165
                )

        )

I would like to add the "quantity" keys and group the "request" key with a comma, output would look like this:

  [0] => Array
        (
            [0] => Array
                (
                    [tag]        => 5x1 Acessori Kids
                    [pedido]     => 6701622422, 6701622409
                    [quantidade] => 5830
                )

        )

    [1] => Array
        (
            [1] => Array
                (
                    [tag]        => RIABASIC5- 5X1 - 2 COMPOSIÇOES
                    [pedido]     => 6701622411
                    [quantidade] => 3165
                )

        )

The sum in the 'quantity' key I got with the following function:

$sum = array_reduce($data, function ($a, $b) {
    isset($a[$b['tag']]) ? $a[$b['tag']]['quantidade'] += $b['quantidade'] : $a[$b['tag']] = $b; 
    return $a;
});

$data = array_values($sum);

but I'm not able to concatenate the 'request' key, which would be for this example above:

 [pedido]     => 6701622422, 6701622409

Does anyone have any tips?

    
asked by anonymous 12.02.2018 / 20:53

3 answers

1

Do as follows:

$sum = array_reduce($arr, function ($a, $b) {
    if (isset($a[$b['tag']])) {
        $a[$b['tag']]['quantidade'] += $b['quantidade'];
        $a[$b['tag']]['pedido'] .= ",{$b['pedido']}";
    } else {
        $a[$b['tag']] = $b;
    }

    return $a;
});

var_dump( $sum );
    
13.02.2018 / 01:11
0

For future searches, I was able to add the 'quantity' key with the following code:

Variable Data is the variable that contains the array:

   $data
    (
        [0] => Array
            (
                [tag] => 5x1 Acessori Kids - 2017
                [pedido] => 6701622409
                [quantidade] => 2125
            )

        [1] => Array
            (
                [tag] => 5x1 Acessori Kids - 2017
                [pedido] => 6701622422
                [quantidade] => 3705
            )

        [2] => Array
            (
                [tag] => RIABASIC5- 5X1 - 2 COMPOSIÇOES
                [pedido] => 6701622411
                [quantidade] => 3165
            )

    )

and below the solution

$sum = array_reduce($data, function ($a, $b) {
    isset($a[$b['tag']]) ? $a[$b['tag']]['quantidade'] += $b['quantidade'] : $a[$b['tag']] = $b;  
    return $a;
});

echo '<pre>';   
print_r(array_values($sum));
echo '</pre>';  

but now still need to add the key 'request', group and not add.

    
12.02.2018 / 23:00
0

With two functions you can get the sum of the elements. array_column () and array_sum () . The code stays that way.

echo array_sum(array_column($data,'quantidade'));

As you said that your version does not support array_column() we can do a foreach () . The code looks like this:

$sum = 0;
foreach ($data as $key) {
    $sum += $key['quantidade'];
}
echo $sum;

Assuming your array is named $data

    
12.02.2018 / 23:06