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...