Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按多个字段对多维数组排序_Php_Sorting_Arrays_Multidimensional Array - Fatal编程技术网

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;   
}

是对多个或多维数组进行排序的特殊函数。我曾经使用过它,并且很喜欢。

是一个特殊的函数,用于对多个或多维数组进行排序。我曾经用过它,我喜欢它。

谢谢。我想我以前试过,但显然不行,因为它奏效了。谢谢。我想我以前试过,但显然不行,因为它奏效了。