我想使用PHP按字母顺序按键排序单行JSON数据.所以最后:
{"one":"morning","two":"afternoon","three":"evening","four":"night"}
Run Code Online (Sandbox Code Playgroud)
变为:
{"four":"night","one":"morning","three":"evening","two":"afternoon"}
Run Code Online (Sandbox Code Playgroud)
我试过用ksort无济于事:
$icons = json_decode(file_get_contents("icons.json"));
ksort($icons);
foreach($icons as $icon => $code){...}
Run Code Online (Sandbox Code Playgroud)
ksort适用于数组,而不是字符串:
$array = json_decode($json, true);
ksort($array);
echo json_encode($array);
Run Code Online (Sandbox Code Playgroud)