Home >Backend Development >PHP Tutorial >PHP realizes multi-dimensional array sorting function sharing of specified fields, multi-dimensional array_PHP tutorial

PHP realizes multi-dimensional array sorting function sharing of specified fields, multi-dimensional array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:04:25895browse

PHP implements multi-dimensional array sorting function sharing for specified fields. Multi-dimensional array

PHP array sorting can be implemented using the array_multisort method, but if it is a multi-dimensional array, and we need to specify the To sort a certain field, 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.
Copy code 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/965357.htmlTechArticlePHP implements multi-dimensional array sorting function sharing for specified fields. Multi-dimensional array PHP array sorting can be implemented using the array_multisort method, but if is a multidimensional array, and we want to specify the array...
Statement:
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