ホームページ >バックエンド開発 >PHPチュートリアル >【問題解決】PHP多次元配列ソート問題
最近 PHP を使って作業していて、多次元配列のソートに関する問題に遭遇しました。この一連の配列にはキーキー PID があり、PID に従って配列を再配置する必要があります。最初のレベルの配列をソートするだけです。 Array_multisort() を使用しましたが、エラーが発生しました。以下の通り:
Array( [0] => Array ( [pid] => 0 [title] => 首页 [path] => index [current] => 0 [link] => /index.php ) [1] => Array ( [tid] => 3 [pid] => 8 [mid] => 8 [topid] => 0 [upid] => 0 [exmid] => 0 [linkid] => 2 [gotoid] => 17 [lng] => cn [larray] => Array ( [0] => Array ( [tid] => 17 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3 ) [1] => Array ( [tid] => 18 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3 ) [2] => Array ( [tid] => 19 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3 ) ) ) [2] => Array ( [tid] => 5 [pid] => 2 [mid] => 3 [topid] => 0 [upid] => 0 [exmid] => 0 [larray] => Array ( [0] => Array ( [tid] => 11 [pid] => 50 [mid] => 3 [topid] => 5 [upid] => 5 ) [1] => Array ( [tid] => 12 [pid] => 50 [mid] => 3 [topid] => 5 [upid] => 5 ) [2] => Array ( [tid] => 22 [pid] => 50 [mid] => 8 [topid] => 5 [upid] => 5 ) ) ) [3] => Array ( [tid] => 50 [pid] => 4 [mid] => 8 [topid] => 0 [upid] => 0 [larray] => ) )
Array( [0] => Array ( [pid] => 0 [title] => 首页 [path] => index [current] => 0 [link] => /index.php ) [1] => Array ( [tid] => 5 [pid] => 2 [mid] => 3 [topid] => 0 [upid] => 0 [exmid] => 0 [larray] => Array ( [0] => Array ( [tid] => 11 [pid] => 50 [mid] => 3 [topid] => 5 [upid] => 5 ) [1] => Array ( [tid] => 12 [pid] => 50 [mid] => 3 [topid] => 5 [upid] => 5 ) [2] => Array ( [tid] => 22 [pid] => 50 [mid] => 8 [topid] => 5 [upid] => 5 ) ) ) [2] => Array ( [tid] => 50 [pid] => 4 [mid] => 8 [topid] => 0 [upid] => 0 [larray] => ) [3] => Array ( [tid] => 3 [pid] => 8 [mid] => 8 [topid] => 0 [upid] => 0 [exmid] => 0 [linkid] => 2 [gotoid] => 17 [lng] => cn [larray] => Array ( [0] => Array ( [tid] => 17 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3 ) [1] => Array ( [tid] => 18 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3 ) [2] => Array ( [tid] => 19 [pid] => 50 [mid] => 8 [topid] => 3 [upid] => 3 ) ) ) )
テストのために var_export(); の後に配列を投稿することをお勧めします
専門家を待って、並べ替えのみを選択します
元の配列は $ar であると仮定します次に、array_multisort(array_map(function($item){ return $item['pid']; }, $ar), $ar);があります
これは print_r が出力するものです