0
我有一个多维数组这可能是这样的:PHP - 排序多维数组的关键
$array[1][0] = "string";
$array[0][1] = "anotherstring";
$array[0][0] = "thirdstring";
$array[1][1] = "fourthstring";
我想排序这个数组由键,以便它看起来像这样:
$array[0][0] = "thirdstring";
$array[0][1] = "anotherstring";
$array[1][0] = "string";
$array[1][1] = "fourthstring";
在我正在使用以下程序的那一刻:
ksort($array);
foreach ($array as $key => $value) {
ksort($value);
$array[$key] = $value;
}
这确实有效,但也许有更好的(内置)函数来做到这一点?
参见[多维阵列上ksort(http://stackoverflow.com/questions/9017418/ksort-多维数组),但我认为你的5行代码就足够了。 – user1477388
array_multisort()是用于多维排序的内置函数。在你的代码中,我不需要'$ array [$ key] = $ value;' – Devon
@Devon需要'$ array [$ key] = $ value;' 'foreach($ array作为&$值)ksort($ value);'不需要 – pesatu