在php中排序数组数组

ray*_*b95 -1 php mysql arrays sorting

我有一个像这样创建的数组:

$i = 0;

$files = array();   

while ($file = mysql_fetch_array($query_files)) {
    $files[$i] = array();
    $files[$i] = $file;
    $i++;
}
Run Code Online (Sandbox Code Playgroud)

我需要能够根据每个$ file ['name']又名$ files [$ i] ['name']对$ files数组进行排序.

我该怎么做呢?谢谢!

cwa*_*ole 8

添加ORDER BY NAME到您的查询.认真.这是最快,最安全,最好的方法.我不是在开玩笑.此外,你要告诉数据库要做一些很棒的事情,而PHP只对它有好处.最后,您可以选择索引DB的name列,而PHP不提供索引.

如果你绝对必须自己排序,那么你需要使用usort.

// this example is almost a clone of something in the docs.
function cmp($a, $b)
{
    return strcmp($a["name"], $b["name"]);
}
usort($files, "cmp");
Run Code Online (Sandbox Code Playgroud)