php多維數組排序方法思路,適用二維、三維、四維.....

最近須要作一個三維排序,查了一下網上,貌似沒有一個比較萬能的方法,因此就本身思考寫了一下,分享一下~php

主要依賴方法:array_multisort 方法文檔:www.php.net/manual/zh/f…數組

方法以下bash

參數:第一個爲要排序的數組,剩下是要排序的鍵(key),和排序方法,鍵的話由於要應對多維的狀況,因此須要上下級鏈接,我這裏採用"."鏈接,(multi_dimension_sort($arr, 'id', SORT_ASC, 'class.class_num', SORT_ASC, 'class.student.value', SORT_DESC)),參數可自行增減,具體看下面的例子ui

function multi_dimension_sort(...$args){
        $arr = array_shift($args); // 取到要排序的數組,剩下的爲要排序的鍵和排序類型
        $sort_arg = [];
        foreach($args as $arg){
            // 這裏主要是爲了獲得排序的key對應的值
            $sort = $arr;
            if(is_string($arg)){
                $arg = explode('.', $arg); // 我設定參數裏面多維數組下的鍵,用‘.’鏈接下級的鍵,這裏獲得鍵,而後下面循環取得數組$arr裏面該鍵對應的值
                foreach($arg as $key){
                    $sort = array_column($sort, $key); // 每次循環$sort的維度就會減一
                }
                $sort_arg[] = $sort;
            }else{
                $sort_arg[] = $arg; // 排序方法SORT_ASC、SORT_DESC等
            }
        }
        $sort_arg[] = &$arr; // 這個數組大體結構爲:[$sort, SORT_ASC, $sort2, SORT_DESC,$arr]
        
        call_user_func_array('array_multisort', $sort_arg); // 由於參數不肯定數量,因此不能直接array_multisort($sort, SORT_ASC, $sort2, SORT_DESC,$arr),用call_user_func_array執行
        
        return($arr);
    }複製代碼

示例:構建一個由學院、班級、學生組成的多維數組,實現按照學院、班級、分數依次來排序spa

$arr = [
            [
                'id' => 2,
                'name' => '學院2',
                'class' => [
                    'class_num' => 2,
                    'student' => [
                        'student_id' => 5,
                        'value' => 20,
                    ]
                ],
            ],[
                'id' => 2,
                'name' => '學院2',
                'class' => [
                    'class_num' => 2,
                    'student' => [
                        'student_id' => 6,
                        'value' => 10,
                    ]
                ],
            ],[    
                'id' => 2,
                'name' => '學院2',
                'class' => [
                    'class_num' => 3,
                    'student' => [
                        'student_id' => 4,
                        'value' => 30,
                    ]
                ],
            ],[    
                'id' => 1,
                'name' => '學院1',
                'class' => [
                    'class_num' => 2,
                    'student' => [
                        'student_id' => 3,
                        'value' => 10,
                    ]
                ],
            ],[
                'id' => 1,
                'name' => '學院1',
                'class' => [
                    'class_num' => 2,
                    'student' => [
                        'student_id' => 4,
                        'value' => 5,
                    ]
                ],
            ],[
                'id' => 1,
                'name' => '學院1',
                'class' => [
                    'class_num' => 3,
                    'student' => [
                        'student_id' => 4,
                        'value' => 10,
                    ]
                ],
            ],[
                'id' => 2,
                'name' => '學院2',
                'class' => [
                    'class_num' => 2,
                    'student' => [
                        'student_id' => 7,
                        'value' => 20,
                    ]
                ],
            ],[
                'id' => 2,
                'name' => '學院2',
                'class' => [
                    'class_num' => 2,
                    'student' => [
                        'student_id' => 1,
                        'value' => 5,
                    ]
                ],
            ]
        ];

        dump(multi_dimension_sort($arr, 'id', SORT_ASC, 'class.class_num', SORT_ASC, 'class.student.value', SORT_DESC));複製代碼

運行結果:.net

array:8 [
  0 => array:3 [
    "id" => 1
    "name" => "學院1"
    "class" => array:2 [
      "class_num" => 2
      "student" => array:2 [
        "student_id" => 3
        "value" => 10
      ]
    ]
  ]
  1 => array:3 [
    "id" => 1
    "name" => "學院1"
    "class" => array:2 [
      "class_num" => 2
      "student" => array:2 [
        "student_id" => 4
        "value" => 5
      ]
    ]
  ]
  2 => array:3 [
    "id" => 1
    "name" => "學院1"
    "class" => array:2 [
      "class_num" => 3
      "student" => array:2 [
        "student_id" => 4
        "value" => 10
      ]
    ]
  ]
  3 => array:3 [
    "id" => 2
    "name" => "學院2"
    "class" => array:2 [
      "class_num" => 2
      "student" => array:2 [
        "student_id" => 5
        "value" => 20
      ]
    ]
  ]
  4 => array:3 [
    "id" => 2
    "name" => "學院2"
    "class" => array:2 [
      "class_num" => 2
      "student" => array:2 [
        "student_id" => 7
        "value" => 20
      ]
    ]
  ]
  5 => array:3 [
    "id" => 2
    "name" => "學院2"
    "class" => array:2 [
      "class_num" => 2
      "student" => array:2 [
        "student_id" => 6
        "value" => 10
      ]
    ]
  ]
  6 => array:3 [
    "id" => 2
    "name" => "學院2"
    "class" => array:2 [
      "class_num" => 2
      "student" => array:2 [
        "student_id" => 1
        "value" => 5
      ]
    ]
  ]
  7 => array:3 [
    "id" => 2
    "name" => "學院2"
    "class" => array:2 [
      "class_num" => 3
      "student" => array:2 [
        "student_id" => 4
        "value" => 30
      ]
    ]
  ]
]複製代碼

先寫到這,有問題再論。code

轉載請註明出處連接: blog.csdn.net/qq_27396589…blog

相關文章
相關標籤/搜索