PHP 多维数组排序


在php中,对于多维数组排序,有可以直接使用的函数,不需要自己重写。array_multisort(),可以看一下文档,php.net上的。w3school和菜鸟的就不要看了,言之不详。


在php中,对于多维数组排序,有可以直接使用的函数,不需要自己重写。

array_multisort(),可以看一下文档,php.net上的。 w3school和菜鸟的就不要看了,言之不详。

举个例子:


$data = [
    ['id' => 13, 'name' => 'sam'],
    ['id' => 8, 'name' => 'tom'],
    ['id' => 46, 'name' => 'addy'],
];
$orderArr = array();
foreach ( $data as $val )
{
    $orderArr[] = $val['id'];
}
array_multisort($orderArr, SORT_ASC, SORT_NUMERIC, $data); 

结果:

blockquote

array(3) { [0]=> array(2) { ["id"]=> int(8) ["name"]=> string(3) "tom" } [1]=> array(2) { ["id"]=> int(13) ["name"]=> string(3) "sam" } [2]=> array(2) { ["id"]=> int(46) ["name"]=> string(4) "addy" } }

pre-code


array(3) {
  [0]=>
  array(2) {
    ["id"]=>
    int(8)
    ["name"]=>
    string(3) "tom"
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(13)
    ["name"]=>
    string(3) "sam"
  }
  [2]=>
  array(2) {
    ["id"]=>
    int(46)
    ["name"]=>
    string(4) "addy"
  }
}

tab缩进

array(3) {
  [0]=>
  array(2) {
    ["id"]=>
    int(8)
    ["name"]=>
    string(3) "tom"
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(13)
    ["name"]=>
    string(3) "sam"
  }
  [2]=>
  array(2) {
    ["id"]=>
    int(46)
    ["name"]=>
    string(4) "addy"
  }
}

xmp

array(3) { [0]=> array(2) { ["id"]=> int(8) ["name"]=> string(3) "tom" } [1]=> array(2) { ["id"]=> int(13) ["name"]=> string(3) "sam" } [2]=> array(2) { ["id"]=> int(46) ["name"]=> string(4) "addy" } }

实例

二维数组排序,一般的做法是我们自己用循环取到排序列,先对排序列排序,在用排序列标识去排序二维数组。

其实PHP内部有自己的二维数组排序方法array_multisort(),举个实例:

// $list数组按time倒序排列
$list = [
    ['name' => '张三', 'phone' => '18405300101', 'time' => 1527647810],
    ['name' => '李四', 'phone' => '18405300103', 'time' => 1527647802],
    ['name' => '王五', 'phone' => '18405300105', 'time' => 1527647817]
];

$orderArr = array();
foreach ($list as $val) {
    $orderArr[] = $val['time'];
}
array_multisort($orderArr, SORT_DESC, SORT_NUMERIC, $list);

print_r($list);

看到这里,大家应该可以发现,array_multisort()只是帮我们封装了foreach循环,里面的思想却是一样的。

函数原型

/**
 * Sort multiple or multi-dimensional arrays
 * @link https://php.net/manual/en/function.array-multisort.php
 * @param array $array1 <p>
 * An array being sorted.
 * </p>
 * @param array|int $array1_sort_order [optional] <p>
 * The order used to sort the previous array argument.
 * Either SORT_ASC to sort ascendingly or SORT_DESC to sort descendingly.
 * This argument can be swapped with array1_sort_flags or omitted entirely, in which case SORT_ASC is assumed.
 * </p>
 * @param array|int $array1_sort_flags [optional] <p>
 * Sort options for the previous array argument:
 * Sorting type flags:
 * SORT_REGULAR - compare items normally (don't change types)
 * SORT_NUMERIC - compare items numerically
 * SORT_STRING - compare items as strings
 * SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
 * SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
 * SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
 * This argument can be swapped with array1_sort_order or omitted entirely, in which case SORT_REGULAR is assumed.
 * </p>
 * @param array|int $_ [optional] <p>
 * More arrays, optionally followed by sort order and flags.
 * Only elements corresponding to equivalent elements in previous arrays are compared.
 * In other words, the sort is lexicographical.
 * </p>
 * @return bool true on success or false on failure.
 */
function array_multisort (array &$array1, $array1_sort_order = null, $array1_sort_flags = null, $_ = null) {}

参考资料

http://php.net/manual/zh/function.array-multisort.php

http://php.net/manual/zh/function.array-multisort.php

http://php.net/manual/zh/function.array-multisort.php

http://www.w3school.com.cn/php/func_array_multisort.asp


返回