0
>Array
>(
>>    [valor_pago_total] => Array
>>>        (
>>>>            [0] => 14.750.861,55
>>>>            [1] => 20.828.923,13
>>>>            [2] => 18.431.681,20
>>>>            [3] => 43.403.452,99
>>>>            [4] => 19.177.285,19
>>>>            [5] => 30.213.514,73
>>>>            [6] => 5.296.079,04
>>>>            [7] => 875.873,76
>>>>            [8] => 254.810,00
>>>)
>>)

Why I execute array_sum($valor_pago_total), I get a wrong result. Why?

2
  • Try removing the periods that are separating the thousands and millions. Commented Jul 11, 2012 at 18:20
  • Can you post more of your code? It looks like you might be calling array_sum on an associative array. Commented Jul 11, 2012 at 18:25

1 Answer 1

1

Get rid of the periods by replacing them with nothing, and make the commas into a period to make array_sum() work. Then use number_format() to turn it back into your formatted result.

Note that the result will need a 64-bit machine.

array_walk( $array['valor_pago_total'], function( &$el) { $el = str_replace( ',', '.', str_replace( '.', '', $el)); });
$sum = array_sum( $array['valor_pago_total']);
var_dump( $sum, number_format( $sum, 2, ',', '.'));

This outputs:

float(153232481.59)
string(14) "153.232.481,59"

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.