Home  >  Article  >  php教程  >  Two-dimensional array sorted by a certain column

Two-dimensional array sorted by a certain column

WBOY
WBOYOriginal
2016-07-06 13:28:271195browse
Jump to [1] [Full screen preview]
/**
 * 二维数组排序方法,数字的倒叙排列
 * @param arr
 * @param 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);
 
    return $array;
}
Statement:
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