Home > Article > Backend Development > PHP method to sort multidimensional arrays based on a certain field, PHP field multidimensional array_PHP tutorial
The example in this article describes the method of php sorting 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
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.