One-dimensional arrays use the sort() function to sort, two-dimensional arrays use the usort() function to sort by internal elements, high-dimensional arrays use the multi-layer nested usort() function to sort by hierarchical elements, and the decomposition problem is solved layer by layer. is the key.
In PHP, array is a powerful data structure that can Stores various types of data, including multidimensional arrays. Multidimensional arrays are arrays that contain other arrays, which allow us to create complex data structures.
Sorting multidimensional arrays can be a challenging task, but understanding the concepts behind it is crucial. In this article, we will take a journey into dimensions and learn how to sort multi-dimensional arrays of one, two and higher dimensions using PHP built-in functions.
One-dimensional array sorting is the simplest form. Using the sort()
function, we can sort the elements in the array in ascending order:
<?php $arr = [5, 2, 8, 3, 1]; sort($arr); print_r($arr); // 输出:[1, 2, 3, 5, 8] ?>
The sorting of two-dimensional arrays is slightly more complicated. We can sort based on the elements in its internal array. For example, suppose we have a two-dimensional array containing student grades:
<?php $students = [ ['name' => 'Alice', 'score' => 90], ['name' => 'Bob', 'score' => 80], ['name' => 'Carol', 'score' => 70] ]; ?>
To sort students in descending order based on their scores, we can use the usort()
function:
<?php usort($students, function($a, $b) { return $b['score'] <=> $a['score']; }); print_r($students); // 输出:[ // ['name' => 'Alice', 'score' => 90], // ['name' => 'Bob', 'score' => 80], // ['name' => 'Carol', 'score' => 70] // ] ?>
The concept of sorting is the same for higher dimensional arrays. For example, let us consider a three-dimensional array containing information about three students' classes:
<?php $classes = [ [ ['name' => 'Alice', 'grade' => 'A'], ['name' => 'Bob', 'grade' => 'B'], ['name' => 'Carol', 'grade' => 'C'] ], [ ['name' => 'Dave', 'grade' => 'A'], ['name' => 'Eve', 'grade' => 'B'], ['name' => 'Frank', 'grade' => 'C'] ], [ ['name' => 'George', 'grade' => 'A'], ['name' => 'Helen', 'grade' => 'B'], ['name' => 'Ian', 'grade' => 'C'] ] ]; ?>
To sort all three classes in descending order based on the students' grades, we can use a multilevel usort( )
Function nesting:
<?php usort($classes, function($a, $b) { usort($a, function($c, $d) { return $d['grade'] <=> $c['grade']; }); usort($b, function($c, $d) { return $d['grade'] <=> $c['grade']; }); return $b[0]['grade'] <=> $a[0]['grade']; }); print_r($classes); // 输出:[ // [ // ['name' => 'Alice', 'grade' => 'A'], // ['name' => 'Bob', 'grade' => 'B'], // ['name' => 'Carol', 'grade' => 'C'] // ], // [ // ['name' => 'Dave', 'grade' => 'A'], // ['name' => 'Eve', 'grade' => 'B'], // ['name' => 'Frank', 'grade' => 'C'] // ], // [ // ['name' => 'George', 'grade' => 'A'], // ['name' => 'Helen', 'grade' => 'B'], // ['name' => 'Ian', 'grade' => 'C'] // ] // ] ?>
The key to understanding multi-dimensional array sorting is to decompose the problem and use nested usort()
functions to solve it layer by layer. This way we can easily sort complex data structures with arbitrary dimensions.
The above is the detailed content of Dimensional journey of PHP multi-dimensional array sorting: from one dimension to multi-dimensional. For more information, please follow other related articles on the PHP Chinese website!