Home > Backend Development > PHP Tutorial > Dimensional journey of PHP multi-dimensional array sorting: from one dimension to multi-dimensional

Dimensional journey of PHP multi-dimensional array sorting: from one dimension to multi-dimensional

王林
Release: 2024-04-29 21:09:02
Original
388 people have browsed it

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.

Dimensional journey of PHP multi-dimensional array sorting: from one dimension to multi-dimensional

The dimensional journey of PHP multi-dimensional array sorting: from one dimension to multi-dimensional

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

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]
?>
Copy after login

Two-dimensional array sorting

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]
];
?>
Copy after login

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]
                        // ]
?>
Copy after login

High Dimension Array Sorting

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']
    ]
];
?>
Copy after login

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']
                        //     ]
                        // ]
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template