搜尋

首頁  >  問答  >  主體

PHP 一個多維數組排序的問題

這個問題有點類似 Mysql 中 order by ,需要的就是在陣列中模擬不同欄位的排序。

假如有以下數組:

$beforeSort = [
    "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
    "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
    "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
];

現在需要在數組中按照 chinese 順序,假如有相同,就按 math 順序,最後得到的應該是如下的數組:

$afterSort = [
    "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
    "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
    "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
];

請問大家有什麼不同的方法可以實現?

滿天的星座滿天的星座2749 天前587

全部回覆(9)我來回復

  • ringa_lee

    ringa_lee2017-05-16 13:10:20

    這是我自己使用的版本,使用方法:

    $afterSort = getArraySort($beforeSort, 'chinese', 'SORT_ASC', 'math', 'SORT_ASC');
    /**
     * 二维数组排序(数字索引数组将重建索引)
     * @param array $arr 需要排序的数组 二维数组
     * @param string $arg1 排序的键名或字段名
     * @param string $arg2 排序的顺序 SORT_ASC或SORT_DESC
     * @param string $arg3 排序的方法 SORT_REGULAR
     * @return array
     */
    function getArraySort($arr, $arg1, $arg2 = "SORT_ASC", $arg3 = "SORT_REGULAR")
    {
        if (!is_array($arr) || !$arr)
        {
            return $arr;
        }
        $argcount = func_num_args();
        for ($i = 1; $i < $argcount; $i++)
        {
            $arg = func_get_arg($i);
            if (!preg_match("/SORT_(.*)/i", $arg))
            {
                $keynamelist[] = $arg;
                $sortrule[] = '$' . $arg;
            }
            else
            {
                $sortrule[] = $arg;
            }
        }
        foreach ($arr AS $key => $info)
        {
            foreach ($keynamelist AS $keyname)
            {
                ${$keyname}[$key] = $info[$keyname];
            }
        }
        $evalstring = 'array_multisort(' . join(",", $sortrule) . ',$arr);';
        eval($evalstring);
        return $arr;
    }

    回覆
    0
  • 迷茫

    迷茫2017-05-16 13:10:20

        $beforeSort = [
            "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
            "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
            "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
        ];
        
        $arr = array();
        foreach($beforeSort as $value) {
            $arr[$value['chinese']][$value['math']] = $value;
        }
        sort($arr);
        
        $result = array();
        foreach($arr as $val) {
            sort($val);
            foreach($val as $vo) {
                $result[] = $vo;
            }
        }
        
        var_dump($result);die;
        

    列印結果:

        array(3) {
          [0]=>
          array(4) {
            ["name"]=>
            string(6) "老王"
            ["english"]=>
            int(30)
            ["chinese"]=>
            int(50)
            ["math"]=>
            int(80)
          }
          [1]=>
          array(4) {
            ["name"]=>
            string(6) "张三"
            ["english"]=>
            int(80)
            ["chinese"]=>
            int(60)
            ["math"]=>
            int(50)
          }
          [2]=>
          array(4) {
            ["name"]=>
            string(6) "李四"
            ["english"]=>
            int(50)
            ["chinese"]=>
            int(60)
            ["math"]=>
            int(70)
          }
        }
        
        
    

    回覆
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:10:20

    可以將數組轉為集合,再處理。使用php的集合實現的sort方法,專治各種複雜排序

    回覆
    0
  • PHPz

    PHPz2017-05-16 13:10:20

    //現在需要在數組中按照 chinese 順序,假如有相同,就按 math 順序,最後得到的應該是如下的數組:
    $beforeSort = [

    "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
    "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
    "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],

    ];

    $data_math = array_column($beforeSort,'math');
    $data_chinese = array_column($beforeSort,'chinese');
    ,$datasultisort($data_chinese,SOSO_Scfore_multisort" ort );

    回覆
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:10:20

    雷雷

    回覆
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:10:20

    ///借用樓上哥們答案

    usort($beforeSort, function ($a, $b) {
        return [$a['chinese'], $b['math']] <=> [$b['chinese'], $a['math']];
    });

    回覆
    0
  • 某草草

    某草草2017-05-16 13:10:20

    對多維數組排序,官方有一個函數可以實作 array_multisort

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:10:20

    雷雷

    回覆
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:10:20

    $排序前 = [

    雷雷

    ];

    foreach ($beforeSort as $key => $value) {

    雷雷

    }
    array_multisort($chinese, SORT_ASC, $math, SORT_ASC, $beforeSort);
    print_r($beforeSort);

    回覆
    0
  • 取消回覆