Avatar
7979 reputation
Posted on:11 Sep '16 - 10:37
-60

PHP array : sum elements of arrays

i have this array like this :

 array(2)(['id'] => '59898441545',['total'] =>array([0] => 2,[1] => 5 [2] => 10 [3] => 35));
I would like the returned array to be the same but, the key 'total' to
not be a array but the sum of all the precedent value like this :
array(2)(['id] => '59898441545',['total'] => 52 // the sum of the precedent elements);

P.S : the number of elements in the 'total array' may change Any help ? thx in advance

PHP

Answers

80
This answer is accepted

$youarray['total'] = array_sum($youarray['total']);
.

Avatar
9057 reputation
Posted on:18 Sep '16 - 10:37

Please login in order to answer a question