我的数组看起来像这样。
Array (
[0] => Array (
[id] => 1
[language] => English
)
[1] => Array (
[id] => 3
[language] => Japanese
)
[2] => Array (
[id] => 4
[language] => Italian
)
[3] => Array (
[id] => 5
[language] => Spanish
)
[4] => Array (
[id] => 6
[language] => French
)
)
现在我要做的是根据语言对这个数组进行排序。所以它应该变成这样。
Array (
[0] => Array (
[id] => 1
[language] => English
)
[1] => Array (
[id] => 6
[language] => French
)
[2] => Array (
[id] => 4
[language] => Italian
)
[3] => Array (
[id] => 3
[language] => Japanese
)
[4] => Array (
[id] => 5
[language] => Spanish
)
)
实际上我确实发现PHP手册很有帮助。它已经解决了。我不知道是否允许我添加问题的答案。但这就是我做到的。我发现PHP的usort对这样的条件很有帮助。 http://hk2.php.net/manual/en/function.usort.php
function build_sorter($key){
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
usort($langs, build_sorter('language'));
foreach ($langs as $value) {
echo '<option value="'.$value['id'].'">'.$value['language'].'</option>';
}
答案 0 :(得分:0)
提取language
列并按该数组对原始数组进行排序:
array_multisort(array_column($array, 'language'), $array);