Home >Backend Development >PHP Tutorial >PHP realizes multi-dimensional array sorting function sharing of specified fields, multi-dimensional array_PHP tutorial
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.