Home > Backend Development > PHP Tutorial > PHP实现指定字段的多维数组排序函数分享_PHP

PHP实现指定字段的多维数组排序函数分享_PHP

WBOY
Release: 2016-05-31 13:13:35
Original
917 people have browsed it

PHP数组排序可以用array_multisort方法实现,但是如果是多维数组,并且我们要指定数组中的某个字段进行排序,那么这就需要我们自己写方法实现了。本文分享了一段PHP指定字段的多维数组排序方法的代码,这段代码可实现根据field字段对数组进行排序。

代码如下:


function sortArrByField(&$array, $field, $desc = false){
  $fieldArr = array();
  foreach ($array as $k => $v) {
    $fieldArr[$k] = $v[$field];
  }
  $sort = $desc == false ? SORT_ASC : SORT_DESC;
  array_multisort($fieldArr, $sort, $array);
}


以上就是PHP指定字段的多维数组排序方法,希望这段PHP代码对你带来帮助。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template