Sum of an array and PHP value concatenation

Asked

Viewed 682 times

0

I have a dynamic array, it contains duplicate keys (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 add the keys "quantity" and group the key "request" with a comma, output would be 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
                )

        )

sum in the quantity key achieved 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);

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

 [pedido]     => 6701622422, 6701622409

anyone has any tips?

  • Already considered updating PHP?

3 answers

1


Do it this way:

$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 );
  • Perfect @Valdeir, thank you very much!

0

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

Date variable 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 I still need to join the key 'request', group and not add.

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 your version does not support array_column() we can make a foreach(). The code stays that way:

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

Assuming your array has the name $data

  • thanks for the return @Phelipe, I tried these two ways, the foreach gives me the sum total of the key, the full array, was not that the expected result, anyway, I get with my answer below the sum of the key 'quantity' grouping the key 'tag', however I am not able to concatenate the 'request' key as my desired output.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.