Php 按多个字段对多维数组排序
我有以下数据:Php 按多个字段对多维数组排序,php,sorting,arrays,multidimensional-array,Php,Sorting,Arrays,Multidimensional Array,我有以下数据: Array ( [0] => Array ( [filename] => def [filesize] => 4096 [filemtime] => 1264683091 [is_dir] => 1 [is_file] => ) [1] => Array ( [filename] => abc
Array (
[0] => Array (
[filename] => def
[filesize] => 4096
[filemtime] => 1264683091
[is_dir] => 1
[is_file] =>
)
[1] => Array (
[filename] => abc
[filesize] => 4096
[filemtime] => 1264683091
[is_dir] => 1
[is_file] =>
)
[2] => Array (
[filename] => rabbit
[filesize] => 4096
[filemtime] => 1264683060
[is_dir] => 0
[is_file] =>
)
[3] => Array (
[filename] => owl
[filesize] => 4096
[filemtime] => 1264683022
[is_dir] => 0
[is_file] =>
)
)
我想按不止一个值对它进行排序。(例如,按is_dir和文件名(按字母顺序)或按filemtime和文件名等)
到目前为止,我已经尝试了很多解决方案,但没有一个有效
有人知道最好的PHP算法/函数/方法来对其进行排序吗?使用并将您自己的比较函数传递给该函数
//example comparison function
//this results in a list sorted first by is_dir and then by file name
function cmp($a, $b){
//first check to see if is_dir is the same, which means we can
//sort by another factor we defined (in this case, filename)
if ( $a['is_dir'] == $b['is_dir'] ){
//compares by filename
return strcmp($a['filename'], $b['filename']);
}
//otherwise compare by is_dir, because they are not the same and
//is_dir takes priority over filename
return ($a['is_dir'] < $b['is_dir']) ? -1 : 1;
}
使用自己的比较函数并将其传递给函数
//example comparison function
//this results in a list sorted first by is_dir and then by file name
function cmp($a, $b){
//first check to see if is_dir is the same, which means we can
//sort by another factor we defined (in this case, filename)
if ( $a['is_dir'] == $b['is_dir'] ){
//compares by filename
return strcmp($a['filename'], $b['filename']);
}
//otherwise compare by is_dir, because they are not the same and
//is_dir takes priority over filename
return ($a['is_dir'] < $b['is_dir']) ? -1 : 1;
}
是对多个或多维数组进行排序的特殊函数。我曾经使用过它,并且很喜欢。是一个特殊的函数,用于对多个或多维数组进行排序。我曾经用过它,我喜欢它。谢谢。我想我以前试过,但显然不行,因为它奏效了。谢谢。我想我以前试过,但显然不行,因为它奏效了。