Home > Backend Development > PHP Tutorial > PHP method to sort multi-dimensional array according to a certain field_PHP tutorial

PHP method to sort multi-dimensional array according to a certain field_PHP tutorial

WBOY
Release: 2016-07-13 10:05:19
Original
876 people have browsed it

How to sort multi-dimensional arrays in PHP based on a certain field

This article mainly introduces the method of sorting multi-dimensional arrays in PHP based on a certain field, and analyzes PHP array operations with examples. and sorting skills, which has certain reference value. Friends in need can refer to it

The example in this article describes how PHP sorts 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

?

1

2

3

4

5

6

7

8

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

}

1 2

3

4 5

67 8
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); }
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/963971.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963971.htmlTechArticlephp method to sort multi-dimensional arrays based on a certain field This article mainly introduces php to sort multi-dimensional arrays based on a certain field Methods for sorting, example analysis of PHP array manipulation and sorting techniques...
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