如何在 json_encoding 时获取数字浮点数(不是字符串!)?

How to get numeric float (not string!) when json_encoding?

我需要一个使用点分隔符的数字浮点数,它在 (json) 编码后保持数字,以便将 POST headers 发送到 third-party API。

但是,已经尝试了几个小时,但无法正常工作。

我需要的是:

{
    "price": 17.95,
    // BUT NOT:
    "price": "17.95" OR 17,95
}

为什么? 因为接收 API 端点检查第一个值,但对后两个值抛出 non-numeric 错误。

现在,我们在荷兰。所以我们的 "locale" 使用逗号分隔符。通过将区域设置从 nl_NL 设置为 en_US 来解决这个问题,为 number_format 函数提供正确的格式,但是,作为字符串。

使用 (float) 转换逗号或点分隔的字符串会导致它丢失分隔点的任何值。 ("17.95" 变成 17)

更新产品详细信息是一个带有几个参数的函数,无需修改 cURL 即可传递这些参数。它将 POST 变量的数组编码为它应该在上面的内容。我只能通过以下内容:

$client->updateShopItem($shopId, $articleNumber, $updateArray)

$shopId = 整数 $articleNumber = 字符串 $updateArray = 数组

完整、正确,调用看起来像:

$client->updateShopItem(12345, "a1b2c3", [
    "price" => 17.95,
    "sale_price" => 12.99,
    //... other values
]);

要代替上面示例中的值使用的值是字符串类型:“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() 对不同 types/values 的影响:

json_encode(["test1" => "17,95", "test2" => 17.95]);

//Results in: 
{"test1":"17,95","test2":17.95}

更新:为避免混淆: 完整代码,仅删除了 non-relevant 内容,否则未经编辑。已包含语言环境 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');
// Update a shopitem
$update2 = [
    'price'             => ((float)number_format(str_replace(',', '.', $prijs), 2)),
    'discount_price'    => (float) str_replace(',', '.', $actieprijs),
];

更新 2:找到解决方案 在与@KevinStich 就他的 发表一些评论后 'n' 发现尝试更改语言环境时的问题在于我在 Windows 上。

他的回答解决了这个问题,在上面需要设置的地方添加了以下代码:

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

发现 in the docs setlocale() 函数 returns false 如果它没有设置新的语言环境,否则 returns 新的语言环境。这导致了上述情况,@KevinStich 的回答奏效了。

@Nukeface 的问题与语言环境设置和基于平台传递给 setlocale() 的不同变量有关;特别是 Windows 使用与 unix 系统不同的语言环境名称。

setlocale 的 return 值可以帮助您诊断调用是否有效,并且您可以使用 localeconv() 查看您当前的一些区域设置。

您可能只想针对 json_encode() 的信息执行此操作,因此请考虑将 setlocale 包装到正确的语言环境、编码,然后将 setlocale 包装回您自己的函数中的标准。

以下是引起评论讨论的旧答案:


您的演员表范围界定似乎有些奇怪。我以前见过这个问题,但不知道该把它归结为什么,我用更多的括号解决了它。

这在 REPL 中对我有用

var_dump(
    ((float)number_format(str_replace(',', '.', $price), 2))
);
float(17.95)

为确保这不是 json_encode 问题:

$price = "17,95";
$a = array();
$a[] = $price;
$a[] = ((float)number_format(str_replace(',', '.', $price), 2));

echo json_encode($a); // Prints ["17,95",17.95]