PHP:使用非字母和非数字键对数组进行排序
我尝试使用PHP的ksort对该数组进行排序:PHP:使用非字母和非数字键对数组进行排序,php,arrays,sorting,ksort,Php,Arrays,Sorting,Ksort,我尝试使用PHP的ksort对该数组进行排序: Array( [district_name] => District name [email] => email@email.com [name] => Name of item [number] => 191 [phone] => +41234568789 [{attr}id] => 2 [questions] => Array(...) ) 但是
Array(
[district_name] => District name
[email] => email@email.com
[name] => Name of item
[number] => 191
[phone] => +41234568789
[{attr}id] => 2
[questions] => Array(...)
)
但是包含
{attr}…
的键不会被排序,它会在其他键被排序时停留在相同的位置。排序此数组的最佳方法是什么?我无法确认这一点。此代码按预期排序(“{attr}id”是结果数组中的最后一个):
请确保您的源数组正常。您希望
{attr}id
位于哪个位置?它在这里排序到数组的末尾。您是对的,一定是我的源代码中的其他内容。谢谢
$arr = array(
"district_name" => "foo",
"email" => "foo",
"name" => "foo",
"number" => "foo",
"phone" => "foo",
'{attr}id' => "foo",
"questions" => "foo",
);
ksort($arr);
var_dump($arr);