I need to have a numeric float using a dot delimiter that remains numeric after (json) encoding to send POST headers to a third-party API.
However, you have tried for several hours but cannot make it work.
What I need:
{
"price": 17.95,
"price": "17.95" OR 17,95
}
Why? . Since the endpoint of the receiving API does the check for the first, but throws an error non-numericfor the last two values.
Now we are in Holland. Therefore, our "locale" uses a comma delimiter. Working around this, setting the locale from nl_NLto en_US, the function number_formatprovides the correct format, however, like a string.
(float) . ("17.95" 17)
- , cURL. POST , . :
$client->updateShopItem($shopId, $articleNumber, $updateArray)
$shopId= int
$articleNumber=
$updateArray=
, , :
$client->updateShopItem(12345, "a1b2c3", [
"price" => 17.95,
"sale_price" => 12.99,
]);
, , : "17,95".
:
$price = "17,95" //Starting point (locale = nl_NL)
number_format($price, 2) // "17.00" - incorrect type and value
number_format(str_replace(',', '.', $price), 2) // "17.95" - string
(float) str_replace(',', '.', $price); // 17,95 - comma
(float) number_format(str_replace(',', '.', $price), 2) //17,95 - comma
setLocale(LC_ALL, 'en_US'); //Changing locale here, US uses dot separator
$check = locale_get_default(); // "en_US"
number_format($price, 2) // "17.00" - incorrect type and value
number_format(str_replace(',', '.', $price), 2) // "17.95" - string
(float) number_format(str_replace(',', '.', $price), 2) // 17,95 - comma
(float) str_replace(',', '.', $price); // 17,95 (can't figure why comma with US locale)
, , json_encode() /:
json_encode(["test1" => "17,95", "test2" => 17.95]);
{"test1":"17,95","test2":17.95}
: : , , . se
setlocale(LC_ALL, 'nl_NL');
ini_set('intl.default_locale', 'nl-NL');
$update = [
'price' => ((float)number_format(str_replace(',', '.', $prijs), 2)),
'discount_price' => (float) str_replace(',', '.', $actieprijs),
];
setlocale(LC_ALL, 'en_US');
ini_set('intl.default_locale', 'en-US');
$update2 = [
'price' => ((float)number_format(str_replace(',', '.', $prijs), 2)),
'discount_price' => (float) str_replace(',', '.', $actieprijs),
];

2:
, @KevinStich , , , Windows.
, , set:
if (!setlocale(LC_ALL, 'en_US.utf8')) { //Works on "normal" server/Linux stuff
setlocale(LC_ALL, 'us'); //Windows is special
}
, setlocale() false, , . , @KevinStich .