Home  >  Article  >  Backend Development  >  The black magic of multidimensional sorting of PHP arrays: revealing the principles behind it

The black magic of multidimensional sorting of PHP arrays: revealing the principles behind it

WBOY
WBOYOriginal
2024-04-29 15:33:02700browse

PHP Multidimensional Array Sorting Black Magic: Use custom function compare_students to sort based on name and grades. Sorting is performed via the usort() function. UAC in PHP 7.4 provides a cleaner way to implement anonymous classes. Custom function compares input arrays and sorts by name in ascending order and grade in descending order.

The black magic of multidimensional sorting of PHP arrays: revealing the principles behind it

The black magic of multi-dimensional sorting of PHP arrays: revealing the secret principle

In PHP, multi-dimensional sorting of arrays is a fun task seemingly complex task. However, mastering the proper techniques and understanding the principles behind it can allow you to master this dark magic with ease.

Practical Case

Suppose we have an array containing student data, and we want to sort the array by last name and grade.

$students = [
    ['name' => 'Alice', 'score' => 90],
    ['name' => 'Bob', 'score' => 85],
    ['name' => 'Charlie', 'score' => 95],
    ['name' => 'Dave', 'score' => 80],
];

Sort based on custom function

We can use the custom function compare_students to compare two student records by last name and grades Sort:

function compare_students($a, $b) {
    if ($a['name'] == $b['name']) {
        return $a['score'] <=> $b['score'];
    }
    return strcmp($a['name'], $b['name']);
}

Then use the usort() function to sort the array:

usort($students, 'compare_students');

Based on UAC

Introduced by PHP 7.4 User-defined anonymous class (UAC), which provides us with another more concise implementation method:

uasort($students, function($a, $b) {
    if ($a['name'] == $b['name']) {
        return $a['score'] <=> $b['score'];
    }
    return strcmp($a['name'], $b['name']);
});

Principle Revealed

Custom Functioncompare_students is a callback function that compares the order of two input arrays, $a and $b, based on their values.

This function first checks whether two students have the same name. If so, it will compare their grades in order to sort them in descending order.

If not, it will use the strcmp() function to compare the students' names in order to sort them in ascending order.

Practical Application

Now, when outputting the $students array, we will get the results sorted by last name and grades:

print_r($students);

Output result:

Array
(
    [0] => Array
        (
            [name] => Alice
            [score] => 90
        )

    [1] => Array
        (
            [name] => Bob
            [score] => 85
        )

    [2] => Array
        (
            [name] => Charlie
            [score] => 95
        )

    [3] => Array
        (
            [name] => Dave
            [score] => 80
        )

)

The above is the detailed content of The black magic of multidimensional sorting of PHP arrays: revealing the principles behind it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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