php二维数组排序

2020-07-28  本文已影响0人  Mracale
<?php
function arraySort($array,$keys,$sort='asc') {
    $newArr = $valArr = array();
    foreach ($array as $key=>$value) {
        $valArr[$key] = $value[$keys];
    }
    ($sort == 'asc') ?  asort($valArr) : arsort($valArr);
    reset($valArr);
    foreach($valArr as $key=>$value) {
        $newArr[$key] = $array[$key];
    }
    return $newArr;
}
$arr = array(
    array('name'=>'张三','score'=>3),
    array('name'=>'张三','score'=>1),
    array('name'=>'张三','score'=>10),
    array('name'=>'张三','score'=>7),
    array('name'=>'张三','score'=>6),
    array('name'=>'张三','score'=>5),
    array('name'=>'张三','score'=>4),
    array('name'=>'张三','score'=>2),
    array('name'=>'张三','score'=>8),
);

$a = arraySort($arr,'score','desc');
echo "<pre>";
print_r($a);
?>
上一篇下一篇

猜你喜欢

热点阅读