This article introduces an example of using the array_multisort function to sort two-dimensional arrays in PHP. Friends in need can refer to it.
Following the previous article: PHP two-dimensional array sorting custom function, today, we introduce another example of PHP two-dimensional array sorting. Sorting two-dimensional arrays in PHP is very simple, mainly using the array_multisort function. Example: <?php /** * php二维数组排序 * edit bbs.it-home.org */ $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']; } array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data); print_r($data); ?> Copy after login Output result: Array ( [0] => Array ( [volume] => 98 [edition] => 2 ) [1] => Array ( [volume] => 86 [edition] => 1 ) [2] => Array ( [volume] => 86 [edition] => 6 ) [3] => Array ( [volume] => 85 [edition] => 6 ) [4] => Array ( [volume] => 67 [edition] => 2 ) [5] => Array ( [volume] => 67 [edition] => 7 ) ) Instructions: The parameters of the array_multisort function are very flexible. You can refer to the instructions in the PHP manual for in-depth study. >>> For more information, please view the complete list of php array sorting methods |