I have a large array. This is an example part of my array:
13 => [
'Discount' => '0.00'
'Total amount' => '50,00'
'Total Net' => '40,00'
]
14 => [
'Discount' => '0.00'
'Total amount' => '20,00'
'Total Net' => '16,00'
]
I need to convert the commas in the numbers to dots. '50,00'
needs to become '50.00'
for example. What's an easy way to do this?
Note: I tried $myArray = str_replace(',', ".", $dataArray);
but this doesnot work.
You have to loop over the array
value's
foreach ($array as $key => $string) {
$array[$key] = str_replace(',', '.', $string);
}
Since it's a multidimensional array you need to loop nested with both the subarray and the value by reference.
Or have the subarray as the input in the str_replace. (u_mulder)
foreach($arr as &$val){
$val = str_replace(",",".", $val);
}
var_dump($arr);
You can also add a check to see if there is a comma in the value before you replace.
Not sure if that will make it faster or slower though.
...
if(strpos($val, ",") !== false) $val = str_replace(",",".", $val);
...
I hope this will help you
array_walk_recursive(
$myarray,
function (&$value) {
$value = str_replace(',', '.', $value);
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.