Home > Backend Development > PHP Tutorial > PHP method to sort multidimensional arrays based on a certain field, PHP field multidimensional array_PHP tutorial

PHP method to sort multidimensional arrays based on a certain field, PHP field multidimensional array_PHP tutorial

WBOY
Release: 2016-07-13 10:05:31
Original
741 people have browsed it

php method to sort multi-dimensional arrays based on a certain field, php field multi-dimensional array

The example in this article describes the method of php sorting multi-dimensional arrays based on a certain field. Share it with everyone for your reference. The specific analysis is as follows:

Sort multi-dimensional arrays according to a certain field. When I saw the role of the array_multisort method, I suddenly thought that this method could be used

This code can sort the array according to the field 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);
}
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963827.htmlTechArticlephp method to sort multi-dimensional arrays according to a certain field, php field multi-dimensional array This article tells the example of php according to a certain field Method for sorting multidimensional arrays. Share it with everyone for your reference...
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