php二维数组排序多为数组排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hai7425/article/details/83987851
<meta charset="gb2312">
<?
//author:qq:874689017
echo "二维数组排序演示";
echo "<hr>";

 $data = array();
    $data[] = array('name'=>'张三1','yuwen' => 67, 'shuxue' => 87);
    $data[] = array('name'=>'张三2','yuwen' => 86, 'shuxue' => 91);
    $data[] = array('name'=>'张三3','yuwen' => 85, 'shuxue' => 76);
    $data[] = array('name'=>'张三4','yuwen' => 98, 'shuxue' => 62);
    $data[] = array('name'=>'张三5','yuwen' => 86, 'shuxue' => 86);
    $data[] = array('name'=>'张三6','yuwen' => 67, 'shuxue' => 76);

    // 取得列的列表
    foreach ($data as $key => $row)
    {
        $name[$key]  = $row['name'];
        $yuwen[$key] = $row['yuwen'];
        $shuxue[$key] = $row['shuxue'];
    }

    array_multisort($yuwen, SORT_DESC,$shuxue, SORT_DESC, $name, SORT_ASC, $data);
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    array_multisort($yuwen, SORT_DESC, $name, SORT_ASC, $data);
    echo "<pre>";
    print_r($data);
    echo "</pre>";


     $data = array();
    $data[] = array('volume' => 67, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 1);
    $data[] = array('volume' => 85, 'edition' => 6);
    $data[] = array('volume' => 98, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 6);
    $data[] = array('volume' => 67, 'edition' => 7);
    // 取得列的列表
    foreach ($data as $key => $row)
    {
        $volume[$key]  = $row['volume'];
        $edition[$key] = $row['edition'];
    }

    $ret = arraySort($data, 'volume', 'desc');

    print_r($ret);

    /**
     * @desc arraySort php二维数组排序 按照指定的key 对数组进行排序
     * @param array $arr 将要排序的数组
     * @param string $keys 指定排序的key
     * @param string $type 排序类型 asc | desc
     * @return array
     */
    function arraySort($arr, $keys, $type = 'asc') {
        $keysvalue = $new_array = array();
        foreach ($arr as $k => $v){
            $keysvalue[$k] = $v[$keys];
        }
        $type == 'asc' ? asort($keysvalue) : arsort($keysvalue);
        reset($keysvalue);
        foreach ($keysvalue as $k => $v) {
           $new_array[$k] = $arr[$k];
        }
        return $new_array;
    }

    //详见https://www.cnblogs.com/M-D-Luffy/p/4224127.html
?>

 

展开阅读全文

没有更多推荐了,返回首页