Home > Article > Backend Development > PHP multi-dimensional array sorting method for specified fields PHP array key value sort PHP array size sort PHP quick sort generation
This article was originally written by Xiaofeng from MaNong.com. Please read the reprint requirements at the end of the article for reprinting. Welcome to participate in our paid contribution plan!
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.
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 PHP specified fields. I hope this PHP code will help you.
The above introduces the multi-dimensional array sorting method of PHP specified fields, including sorting methods and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.