Home > Article > Backend Development > PHP function to sort two-dimensional array according to specified fields
PHP One-dimensional data sorting, we all know, just use the various array functions that come with PHP (such as arsort(), asort(), krsort(), asort(), etc.), and two-dimensional data Sorting can be achieved according to the following custom methods.
/** * 二维数组根据字段进行排序 * @params array $array 需要排序的二维数组 * @params string $field 排序的字段 * @params string $sort 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序 */ function arraySequence($array, $field, $sort = 'SORT_DESC') { $arrSort = array(); foreach ($array as $uniqid => $row) { foreach ($row as $key => $value) { $arrSort[$key][$uniqid] = $value; } } array_multisort($arrSort[$field], constant($sort), $array); return $array; }
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of PHP function to sort two-dimensional array according to specified fields. For more information, please follow other related articles on the PHP Chinese website!