Jump to content

[SOLVED] array_sum ignores commas


xiao

Recommended Posts

you can just make your own function. This one will add up the numbers for you, just put the number in an array.

 

<?php
function add_numbers($array){
  if(is_array($array)){
    foreach($array as $n){
    $numbers[] = str_replace(",", ".", $n);
    }
  $total = number_format(array_sum($numbers), 2, ',', ' ');
  } else {
  $total = "Numbers should be in an array";
  }
return $total;
}

$numbers = array("5,7", "5,9", "5,8");

echo add_numbers($numbers);
?>

 

Ray

 

I know this is marked solved, but you can do the same thing using the array_map() function:

<?php
function comma_dot($str) {
return(str_replace(',','.',$str));
}

$numbers = array("5,7", "5,9", "5,8");
echo number_format(array_sum(array_map('comma_dot',$numbers)),2,',',' ');
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.