Home  >  Article  >  Backend Development  >  How to sort multiple arrays using PHP

How to sort multiple arrays using PHP

PHPz
PHPzOriginal
2023-04-20 13:53:44572browse

PHP is a very popular development language that is widely used in the development of websites and applications. In PHP, arrays are one of the most commonly used data types and we can use them to organize and store various data. When we need to sort multiple arrays, we need to understand how to sort multiple arrays using PHP and how to combine them with other operations.

  1. sort() function

The sort() function is one of the most commonly used array sorting functions in PHP. It sorts the array in ascending order (smallest to largest). The following is an example:

$numbers = array(5, 3, 7, 1, 9);
sort($numbers);
print_r($numbers);

The output of running the above code is as follows:

Array
(
    [0] => 1
    [1] => 3
    [2] => 5
    [3] => 7
    [4] => 9
)

In the above example, we first declare an array named $numbers, which contains five integers. We then use the sort() function to sort the array in ascending order, and use the print_r() function to output the sorted results.

It should be noted that the sort() function will modify the order of the original array instead of creating a new sorted array. If you need to preserve the order of the original array, you can copy it into a new array and sort it.

  1. asort() function

asort() function can sort the array in ascending order, but it will also retain the relationship of key-value pairs. The following is an example:

$students = array("Tom" => 75, "John" => 82, "Sarah" => 68, "Peter" => 93);
asort($students);
print_r($students);

The output of running the above code is as follows:

Array
(
    [Sarah] => 68
    [Tom] => 75
    [John] => 82
    [Peter] => 93
)

In the above example, we declare an associative array named $students, which contains four named Keys and corresponding values ​​for "Tom", "John", "Sarah", and "Peter". We use the asort() function to sort the array, and the print_r() function to output the sorted results.

Similar to the sort() function, the asort() function will also modify the order of the original array. If you need to preserve the order of the original array, you can use the ksort() function for key sorting.

  1. array_multisort() function

The array_multisort() function can sort multiple arrays and supports sorting the values, keys, or both in the array at the same time. The following is an example:

$name = array("Tom", "John", "Sarah", "Mary");
$score = array(75, 82, 68, 90);
array_multisort($score, $name);
print_r($name);

The output of running the above code is as follows:

Array
(
    [0] => Sarah
    [1] => Tom
    [2] => John
    [3] => Mary
)

In the above example, we declared two arrays: $name and $score. We use the array_multisort() function to sort the $score array, and then rearrange the $name array according to the sorted order. Finally, we use the print_r() function to output the $name array.

It should be noted that the array_multisort() function will modify the order of the original array instead of creating a new sorted array.

  1. usort() function

usort() function is a function used for custom sorting in PHP. It allows us to sort an array using a comparison function we wrote ourselves. The following is an example:

$students = array(
    array("name" => "Tom", "score" => 75),
    array("name" => "John", "score" => 82),
    array("name" => "Sarah", "score" => 68),
    array("name" => "Peter", "score" => 93)
);

function compare($a, $b) {
    return $a["score"] < $b["score"] ? -1 : 1;
}

usort($students, "compare");

foreach ($students as $student) {
    echo $student["name"] . " : " . $student["score"] . "<br>";
}

The output of running the above code is as follows:

Sarah : 68
Tom : 75
John : 82
Peter : 93

In the above example, we declare a two-dimensional array $students containing four elements. We also wrote a comparison function compare() which takes two parameters $a and $b and then compares their scores and returns -1 or 1 depending on the comparison result.

We use the usort() function to call the compare() function to sort the $students array. Finally, we use a foreach() loop to loop through the sorted array and use an echo statement to output the name and score of each student.

Summary

There are many ways to sort arrays in PHP, and you can choose the corresponding function according to your needs. The sort() function can be used to sort the array in ascending order. The asort() function can be used to sort the array in ascending order and retain key-value relationships. The array_multisort() function can sort multiple arrays. The usort() function can be customized. Sort. Note that these functions modify the order of the original array rather than creating a new sorted array. If you need to preserve the order of the original array, you can copy it into a new array and sort it.

The above is the detailed content of How to sort multiple arrays using PHP. 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