如上所述,qazxsw poi PHP qazxsw poi函数将布尔值转换为结果字符串中的整数。
有没有办法:
here
结果可能是http_build_query
主要问题是因为我有一个JSON,应该转换为查询字符串和参数排序。我的逻辑是:
$a = ["teste1" => true, "teste2" => false];
echo http_build_query($a);
但结果字符串包含teste1=true&teste2=false
和$json = file_get_contents('php://input');
$decoded = json_decode($json, true);
$query = http_build_query($decoded);
$exploded = explode('&', $query);
sort($exploded);
$params = implode('&', $exploded);
而不是0
和1
。你有什么建议吗?
将JSON解码为数组后,
false
您可以使用true
按键对其进行排序。然后你只需要使用$json = file_get_contents('php://input');
$decoded = json_decode($json, true);
一次而不是内爆/爆炸等。
ksort
我为我的问题找到了解决方案,我忘了提到JSON被解码为多维数组,所以:
http_build_query
我仍然坚持爆炸/内爆解决方案,这是因为ksort($decoded);
$params = http_build_query(array_map('boolsToString', $decoded));
function boolsToString ($value) {
if ($value === true) return 'true'; // strict comparison with === is necessary
if ($value === false) return 'false';
return $value;
}
正确地制作了多维键,如:function normalizeArray($array) {
foreach ($array as &$el) {
if (is_bool($el)) {
$el = ($el) ? "true" : "false";
} elseif (is_array($el)) {
$el = normalizeArray($el);
}
}
return $array;
}
$json = file_get_contents('php://input');
$decoded = json_decode($json, true);
$normalized = normalizeArray($decoded);
$query = http_build_query($normalized);
$exploded = explode('&', $query);
sort($exploded);
$params = implode('&', $exploded);
和以前的解决方案我丢失了“父级”键名。