Answer the question
In order to leave comments, you need to log in
Does json_decode function reformat numbers?
I encountered strange behavior in my code:
1. a certain json arrives, from which I expect to get an array.
if ($data['prices']) {
$prices = json_decode($data['prices'],true);
string(28) "{"currency":"UAH","1":152.7}" string(28) "{"currency":"UAH","1":152.7}"
array(2) { ["currency"]=> string(3) "UAH" [1]=> float(152,7) } array(2) { ["currency"]=> string(3) "UAH" [1]=> float(152,7) }
Answer the question
In order to leave comments, you need to log in
Probably the locale is taken into account.
You can use the JSON_BIGINT_AS_STRING flag and then floatval() if needed.
setlocale(LC_ALL, 'ru_RU.utf8');
Header("Content-Type: text/html;charset=UTF-8");
first, convert the number to a string,
then replace the commas with dots
with a regular expression, and then the code to json
Do you like to do checks?
Check whether the original number through === is equal to what happened. If not, the commas are replaced. If yes, the comma either did not exist or was not replaced.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question