我有这个输出:
Array
(
[0] => Array
(
[id] => 4
[username] => bla1
)
[1] => Array
(
[id] => 5
[username] => bla2
)
[2] => Array
(
[id] => 6
[username] => bla3
)
)
如何通过' username'?
对这个外部数组中的3个数组进行排序这是我试过的:
if($this->needOrder) {
$sorted = [];
foreach($files as $file) {
$sorted[] = asort($tableFile);
}
return $sorted;
}
$files
具有上述代码的内容。 asort()
仅用于测试是否有效。
但我的回归是:
Array
(
[0] => 1
[1] => 1
[2] => 1
)
答案 0 :(得分:1)
使用usort()
$arr = Array(
array('id' => 4,'username' => 'bla2'),
array('id' => 5,'username' => 'bla3'),
array('id' => 6,'username' => 'bla1'),
);
function custom_sort($a, $b)
{
return strnatcmp($a['username'], $b['username']);
}
usort($arr, "custom_sort");
print '<pre>';
print_r($arr);
print '</pre>';
<强>输出:强>
Array
(
[0] => Array
(
[id] => 4
[username] => bla1
)
[1] => Array
(
[id] => 4
[username] => bla2
)
[2] => Array
(
[id] => 4
[username] => bla3
)
)