Home > Backend Development > PHP Tutorial > PHP function to sort two-dimensional array according to specified fields

PHP function to sort two-dimensional array according to specified fields

angryTom
Release: 2023-04-07 15:48:02
forward
2600 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:www.whmblog.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template