PHP implementation of multi-dimensional array sorting function sharing for specified fields_PHP tutorial

WBOY
Release: 2016-07-13 10:04:20
Original
761 people have browsed it

Sharing of the multi-dimensional array sorting function of specified fields in PHP The code of the multi-dimensional array sorting method. This code can sort the array according to the field field. Friends who need it can refer to it

PHP array sorting can be implemented using the array_multisort method, but if it is a multi-dimensional array and we want to specify a field in the array for sorting, then we need to write our own method to implement it. This article shares a piece of code for the multi-dimensional array sorting method of specified fields in PHP. This code can sort the array according to the field field.
The code is as follows:

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

The above is the multi-dimensional array sorting method of specified fields in PHP. I hope this PHP code will help you.

http://www.bkjia.com/PHPjc/965525.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/965525.htmlTechArticlePHP implements multi-dimensional array sorting function of specified fields. Share this article mainly introduces PHP to implement multi-dimensional array sorting of specified fields. Function sharing, this article shares a multi-dimensional PHP specified field...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!