美文网首页
PHP二维数组按照字段排序的三种方法!

PHP二维数组按照字段排序的三种方法!

作者: DragonersLi | 来源:发表于2017-08-04 18:17 被阅读34次

    方法一:

        /*二维数组排序
        * @param $multi_array 要排序的二维数组
        * @param $sort_key 要排序的二维数组字段
        * @param $sort 排序(0:asc;1:desc)
        *
        */
        function multi_array_sort($multi_array,$sort_key,$sort){
            $sort = $sort ? SORT_DESC : SORT_ASC;
            if(is_array($multi_array)){
                foreach ($multi_array as $row_array){
                    if(is_array($row_array)){
                        $key_array[] = $row_array[$sort_key];
                    }else{
                        return false;
                    }
                }
            }else{
                return false;
            }
            array_multisort($key_array,$sort,$multi_array);
            return $multi_array;
        }
    

    示例一:

    $arr = array(
    'd' => array('id' => 5, 'name' =>'f', 'age' => 7),
    'b' => array('id' => 2,'name' => 'c','age' => 4),
    'a' => array('id' => 8,'name' => 'a','age' => 5),
    'c' => array('id' => 1,'name' => 'e','age' => 2)
    ); 
    print_r(multi_array_sort($arr,'name',true));exit;
    
    
    

    方法二:

     //二维数组排序, $arr是数据,$keys是排序的健值,$order是排序规则,1是升序,0是降序
    function array_sort_by_any_row($arr, $keys, $order=0) {
        if (!is_array($arr)) return false; 
        $keysvalue = array();
        foreach($arr as $key => $val) {
            $keysvalue[$key] = $val[$keys];
        }
        $order ? arsort($keysvalue) : asort($keysvalue);
     
        reset($keysvalue);
        foreach($keysvalue as $key => $vals) {
            $keysort[$key] = $key;
        }
        $new_array = array();
        foreach($keysort as $key => $val) {
            $new_array[$key] = $arr[$val];
        }
        return $new_array;
    } 
    
    

    示例二:

    $arr = array(
    'd' => array('id' => 5, 'name' =>'f', 'age' => 7),
    'b' => array('id' => 2,'name' => 'c','age' => 4),
    'a' => array('id' => 8,'name' => 'a','age' => 5),
    'c' => array('id' => 1,'name' => 'e','age' => 2)
    ); 
    print_r(array_sort_by_any_row($arr,'name',true));exit;
    
    

    方法三:

        
    /**
    *在某种程度上对二维数组进行排序,使用arraymultisort()函数。 
    * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
    * @author Chunsheng Wang <wwccss@263.net>
    * @param数组$ArrayData将数组排序。
    * @param字符串$KeyName1是第一个排序的条目。
    * @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC")
    * @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
    * @返回数组排序数组。
    
    */
    function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
    {
        if(!is_array($ArrayData)){
            return $ArrayData;
        }
            $ArgCount = func_num_args();
        for($I = 1;$I < $ArgCount;$I ++){
            $Arg = func_get_arg($I);
            if(!@eregi("SORT",$Arg))
            {
                $KeyNameList[] = $Arg;
                $SortRule[] = '$'.$Arg;
            }else{
                $SortRule[] = $Arg;
            }
        }
        foreach($ArrayData AS $Key => $Info){
            foreach($KeyNameList AS $KeyName){
                ${$KeyName}[$Key] = $Info[$KeyName];
            }
        }
        $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
        eval ($EvalString);
        return $ArrayData;
    }
    

    示例三:

    $arr = array(
    array(
    'name' => '学习',
    'size' => '1235',
    'type' => 'jpe',
    'time' => '1921-11-13',
    'class' => 'D',
    ),
    array(
    'name' => '中国功夫',
    'size' => '153',
    'type' => 'jpe',
    'time' => '2005-11-13',
    'class' => 'J',
    ),
    array(
    'name' => '编程',
    'size' => '35',
    'type' => 'gif',
    'time' => '1997-11-13',
    'class' => 'A',
    ),
    array(
    'name' => '中国功夫',
    'size' => '65',
    'type' => 'jpe',
    'time' => '1925-02-13',
    'class' => 'D',
    ),
    array(
    'name' => '中国功夫',
    'size' => '5',
    'type' => 'icon',
    'time' => '1967-12-13',
    'class' => 'C',
    ),
    );
    
    
    
    echo "<pre>";print_r($arr);
    
     
    //$temp = sysSortArray($arr,"class","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING");
    //$temp = sysSortArray($arr,"class","SORT_DESC");//按class字段升降序:SORT_ASC,SORT_DESC
    //$temp = sysSortArray($arr,"time","SORT_ASC");//按time字段日期大小升降序:SORT_ASC,SORT_DESC 
    //$temp = sysSortArray($arr,"type","SORT_DESC");//按type字段升降序:SORT_ASC,SORT_DESC
    //$temp = sysSortArray($arr,"size","SORT_ASC");//按size字段值大小升降序:SORT_ASC,SORT_DESC 注意:按照数字方式排序时 153 比 65 小
    //$temp = sysSortArray($arr,"name","SORT_DESC");//按name字段中文字母升降序:SORT_ASC,SORT_DESC
    
    $temp = sysSortArray($arr,"size","SORT_ASC","time","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING"); //组合情况
    echo "<pre>";
    print_r($temp); 
    
    
    
    

    相关文章

      网友评论

          本文标题:PHP二维数组按照字段排序的三种方法!

          本文链接:https://www.haomeiwen.com/subject/xdrblxtx.html