In PHP programming, array sorting is a very common requirement. When sorting an array, we need to know how to distinguish the size of elements. This article will introduce how to size arrays in PHP and how to sort arrays.
Size comparison of array elements
In PHP, we can use ==, ===, !=, <>, !==, <, >, < ;=, >= These operators perform size comparisons of elements. However, these operators can be used on scalar data types (such as int, float, string, etc.), but there will be some problems when used on arrays.
When using <, >, <=, >= operators to compare arrays, PHP will internally convert the array into a string, and then compare the sizes of the strings, which can easily occur Unexpected results.
In order to avoid such problems, we need to use special functions to compare elements of arrays. Here are some commonly used functions.
strcmp function
The strcmp function is used to compare the sizes of two strings and return an integer. If the first string is less than the second string, a negative number is returned; if the two strings are equal, 0 is returned; if the first string is greater than the second string, a positive number is returned.
For example, we can use the strcmp function to compare the sizes of the two words "hello" and "world", as follows:
echo strcmp("hello", "world"); // 输出 -15
In this example, "hello" is smaller than "world", Therefore the strcmp function returns a negative number.
sort function
The sort function is used to sort an array in ascending order. The sort function sorts the array in place, that is, the key values of the array will not change after sorting.
The sort function accepts an array parameter, for example:
$numbers = array(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5); sort($numbers); print_r($numbers); // 输出 Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 3 [4] => 3 [5] => 4 [6] => 5 [7] => 5 [8] => 5 [9] => 6 [10] => 9 )
In the above example, the sort function sorts the $numbers array in ascending order and prints the sorted results.
rsort function
The rsort function is the same as the sort function and is used to sort arrays. The difference is that the rsort function is used to sort an array in descending order.
The rsort function also performs in-place operations on arrays, for example:
$numbers = array(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5); rsort($numbers); print_r($numbers); // 输出 Array ( [0] => 9 [1] => 6 [2] => 5 [3] => 5 [4] => 5 [5] => 4 [6] => 3 [7] => 3 [8] => 2 [9] => 1 [10] => 1 )
In the above example, the rsort function sorts the $numbers array in descending order and prints the sorted results. .
array_multisort function
array_multisort function is a powerful sorting function that can sort multi-dimensional arrays. The array_multisort function accepts one or more array arguments and sorts these arrays according to specified rules.
For example, we can use the array_multisort function to sort the scores of a group of students:
$students = array( array('name' => 'Tom', 'score' => 90), array('name' => 'John', 'score' => 80), array('name' => 'Mike', 'score' => 85), array('name' => 'Mary', 'score' => 95), array('name' => 'Lisa', 'score' => 88) ); foreach ($students as $key => $value) { $scores[$key] = $value['score']; $names[$key] = $value['name']; } array_multisort($scores, SORT_DESC, $names, SORT_ASC, $students); print_r($students);
In the above example, we first store the students' scores and names into two arrays $ scores and $names. We then use the array_multisort function to sort the $scores and $names arrays and apply the sorted results to the $students array. Finally, we print the $students array to get the results sorted by scores in descending order and names in ascending order.
Summary
When sorting an array in PHP, we need to know how to distinguish the size of the elements. To avoid unexpected results, we need to use specialized functions for comparison. This article introduces some commonly used comparison functions and sorting functions, which can help readers quickly write efficient code.
The above is the detailed content of How to distinguish the size of php array sorting. For more information, please follow other related articles on the PHP Chinese website!