排序是將陣列的元素依照特定的順序排列。 PHP 對普通數組(如數值數組)和關聯數組執行排序。像數字數組這樣的普通數組可以使用簡單的 sort() 函數進行排序,並且為了對關聯數組執行排序,我們有不同的函數。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
排序可以按升序或降序、字母或數字順序、自然方式、隨機以及使用者定義的順序進行。對於數值數組或索引數組等數組以及關聯數組,排序是基於鍵或基於任意兩個順序(例如昇序或降序)中的值按升序或降序數組進行排序。如果資料元素採用排序形式,則對陣列進行排序可以使您的搜尋更加輕鬆。
排序是在 PHP 中使用排序函數執行的。有多種排序功能。
假設您想根據資歷了解家庭成員的年齡。一個家庭可能有 15 名成員。為了對 15 名成員的年齡進行排序,我們使用排序函數並快速得到結果。因此,在這種情況下,排序就顯得特別重要。
此外,不需要使用任何函式庫。
文法:
sort(array);
其中 array 是輸入陣列的名稱。
以下範例將 $people 和 $ages 陣列的元素排序。
依字母順序排序:
$people = array ('Rama', 'James', 'Mary', 'Alice', 'Radha');
依數字順序排序:
$ages = array (25,10,30,15,20);
組合以上兩個陣列並建立一個關聯。
$people_ages = array ('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20);
依數字順序排序,範例:
代碼:
<?php //example to perform ages array $ages = array(25,10,30,15,20); // calculate length of array $array_length = count($ages); echo "Before Sort"."<br>"; //array before sorting for($i=0;$i<$array_length;$i++) { echo $ages[$i] ."<br>"; } echo '<hr>'; //performing sort sort($ages); echo "After Sort"."<br>"; //array after sorting for($i=0;$i<$array_length;$i++) { echo $ages[$i]."<br>"; } ?>
輸出:
按字母順序排序,範例:
代碼:
<?php //example to perform people array $people= array('Rama', 'James', 'Mary', 'Alice', 'Radha'); // calculate length of array $array_length = count($people); echo "Before Sort"."<br>"; //array before sorting for($i=0;$i<$array_length;$i++) { echo $people[$i] ."<br>"; } echo '<hr>'; //performing sort sort($people); echo "After Sort"."<br>"; //array after sorting for($i=0;$i<$array_length;$i++) { echo $people[$i]."<br>"; } ?>
輸出 :
對具有鍵值對關聯的關聯數組執行排序將導致鍵遺失。此外,雖然執行了排序,但陣列的每個元素現在都被分配了一個新的數字索引。
代碼:
// example to perform sort on people and ages array together // you will find that the keys are not preserved and changed $people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20); // calculate length of array $array_length = count($people_ages); echo "Before Sort"."<br>"; //array before sorting we will use foreach loop foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort sort($people_ages); echo "After Sort"."<br>"; //array after sorting foreach ($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; }
輸出:
因此,我們使用 asort(),而不是簡單的 sort()。 asort() 是一個按升序對關聯數組的元素進行排序的函數。 arsort() 是一個對陣列元素進行降序排序的函數。兩者均按數值排序。現在讓我們詳細了解這些數組以及其他數組函數
下面提到了不同類型的陣列函數,以及排序的順序,無論是升序還是降序,以及函數按鍵排序或按值排序。
Let us learn about each function in detail
This function we have already seen. This function performs sorting on the given array and arranges the elements of the array in ascending array.
Code :
//example to perform ages array $ages = array(25,10,30,15,20); // calculate length of array $array_length = count($ages); echo "Before Sort"."<br>"; //array before sorting for($i=0;$i<$array_length;$i++) { echo $ages[$i] ."<br>"; } echo '<hr>'; //performing sort sort($ages); echo "After Sort"."<br>"; //array after sorting for($i=0;$i<$array_length;$i++) { echo $ages[$i]."<br>"; }
Output:
This function performs sorting on the given array and arranges the elements of the array in descending array, opposite of what sort() function does. Also, the sorting is performed with values.
a. Code:
//example to perform ages array $ages = array(25,10,30,15,20); // calculate length of array $array_length = count($ages); echo "Before Sort"."<br>"; //array before sorting for($i=0;$i<$array_length;$i++) { echo $ages[$i] ."<br>"; } echo '<hr>'; //performing sort rsort($ages); echo "After Sort"."<br>"; //array after sorting for($i=0;$i<$array_length;$i++) { echo $ages[$i]."<br>"; }
Output :
b. Code:
//example to perform people array $people= array('Rama', 'James', 'Mary', 'Alice', 'Radha'); // calculate length of array $array_length = count($people); echo "Before Sort"."<br>"; //array before sorting for($i=0;$i<$array_length;$i++) { echo $people[$i] ."<br>"; } echo '<hr>'; //performing sort rsort($people); echo "After Sort"."<br>"; //array after sorting for($i=0;$i<$array_length;$i++) { echo $people[$i]."<br>"; }
Output:
This function performs sorting on the given array and arranges the array’s values in ascending order, opposite of what sort() function does. Also, the sorting is performed with values and not keys.
Code :
//example to perform people_ages array $people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20); // calculate length of array $array_length = count($people_ages); echo "Before Sort"."<br>"; //array before sorting foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort asort($people_ages); echo "After Sort"."<br>"; //array after sorting foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; }
Output:
This function performs sorting on the given array and arranges the array’s values in a descending array. This example prints the array using a foreach loop and outputs the result as before sorting and after sorting.
Code:
//example to perform people_ages array $people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20); // calculate length of array $array_length = count($people_ages); echo "Before Sort"."<br>"; //array before sorting foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort arsort($people_ages); echo "After Sort"."<br>"; //array after sorting foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; }
Output:
This function performs sorting on the given array and arranges the keys of the array in ascending order. This example prints the array using a foreach loop and outputs the result as before sorting and after sorting.
Code:
//example to perform people_ages array $people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20); // calculate length of array $array_length = count($people_ages); echo "Before Sort"."<br>"; //array before sorting foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort ksort($people_ages); echo "After Sort"."<br>"; //array after sorting foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; }
Output:
This function performs sorting on the given array and arranges the keys of the array in descending order. This example prints the array using a foreach loop and outputs the result as before sorting and after sorting.
Code:
//example to perform people_ages array $people_ages = array('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20); // calculate length of array $array_length = count($people_ages); echo "Before Sort"."<br>"; //array before sorting foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort krsort($people_ages); echo "After Sort"."<br>"; //array after sorting foreach($people_ages as $key=>$value) { echo $key."=>".$value."<br>"; }
Output:
This function performs sorting on the given array and arranges the keys of the array in descending order. This example prints the array using a foreach loop and outputs the result as before sorting using assort() function and after sorting using natsort() function.
This function refreshes the output as the function randomizes the order of values in the given array. New numeric keys replace the keys mentioned in the array are assigned. For example, 10 is greater than 7 in a human being view, but according to the sorting algorithm 10 comes before 7.
We will use the natural flow of order.
Code:
<?php $input = array("13 orange","14 Apple","15 3Banana","11 papaya","10 Grapes");; $arr1 = $arr2 = $input; echo "Before Sort"."<br>"; //array before sorting foreach($input as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort sort($arr1); echo "Using asort function "."<br>"; //array before sorting foreach($arr1 as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort natsort($arr2); echo "Using natsort function "."<br>"; foreach($arr2 as $key=>$value) { echo $key."=>".$value."<br>"; } ?>
Output :
This function works the same as natsort() but is case insensitive.
Code:
$input = array("13 orange","14 Apple","15 Banana","11 papaya","10 Grapes");; $arr1 = $arr2 = $input; echo "Before Sort"."<br>"; //array before sorting foreach($input as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort sort($arr1); echo "Using asort function "."<br>"; //array before sorting foreach($arr1 as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; //performing sort natcasesort($arr2); echo "Using natcasesort function "."<br>"; foreach($arr2 as $key=>$value) { echo $key."=>".$value."<br>"; }
Output :
This function performs sorting on the given array and arranges the values of the array in ascending order. This example prints the array using for loop and outputs the result.
In this program, the usort function takes two parameters: the input array and the other is the name of the function being called (here is compare).
This compare function is user-defined; also, the function is optional. This function returns 0 only if the condition in if block is satisfied, and else it will send -1 if the values compared is smaller than the other and 1 if the values compared is greater than the other.
Code:
function compare($x, $y) { if($x == $y ){ return 0; } if($x < $y ){ return -1; } if($x > $y ){ return 1; } } $numbers = array(10,4,5,3,20); echo "Before Sort"."<br>"; //array after sorting $array_length = count($numbers); for($i=0;$i<$array_length;$i++) { echo $numbers[$i]."<br>"; } echo '<hr>'; //performing sort usort($numbers, "compare"); echo "After Sort"."<br>"; //array after sorting $array_length = count($numbers); for($i=0;$i<$array_length;$i++) { echo $numbers[$i]."<br>"; }
Output :
This function performs sorting on the given array and arranges the array’s values in ascending order using the compare function.
Code:
<?php function compare($x, $y) { if($x == $y ){ return 0; } if($x < $y ){ return -1; } if($x > $y ){ return 1; } } echo '<hr>'; //performing sort $input = array("num1"=>10,"num2"=>4,"num3"=>3,"num4"=>5, "num5"=>20); uasort($input, "compare"); echo "After Sort"."<br>"; //array after sorting $array_length = count($input); foreach($input as $key=>$value) { echo $key."=>".$value."<br>"; }
Output:
This function performs sorting on the given array and arranges the array’s keys in ascending order using the compare function.
Code:
<?php function compare($x, $y) { if($x == $y ){ return 0; } if($x < $y ){ return -1; } if($x > $y ){ return 1; } } echo '<hr>'; //performing sort $input = array("num1"=>10,"num2"=>4,"num3"=>3,"num4"=>5, "num5"=>20); uksort($input, "compare"); echo "After Sort"."<br>"; //array after sorting $array_length = count($input); foreach($input as $key=>$value) { echo $key."=>".$value."<br>"; }
Output :
This function refreshes the output as the function randomizes the order of values in the given array. New numeric keys replace the keys mentioned in the array are assigned.
Code:
$input = array('a'=>"Guava",'e'=>"Apple",'b'=>"Orange",'c'=>"Papaya", 'd' => "Banana"); echo "Before Sort"."<br>"; //array before sorting foreach($input as $key=>$value) { echo $key."=>".$value."<br>"; } echo '<hr>'; shuffle($input); echo 'You need to refresh to see the new shuffle everytime'.'<br>'; $array_length = count($input); echo '<hr>'; //array after sorting $array_length = count($input); foreach($input as $key=>$value) { echo $key."=>".$value."<br>"; }
Output:
In this article, most of the types of sorting are covered. The arrays are explained with examples. I hope you find it useful, informative and interesting.
以上是PHP 中的排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!