並べ替えとは、配列の要素を特定の順序で並べることです。 PHP は、数値配列などの通常の配列と連想配列に対してソートを実行します。数値配列のような通常の配列は、単純な sort() 関数を使用して並べ替えることができます。また、連想配列で並べ替えを実行するには、さまざまな関数があります。
広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト無料ソフトウェア開発コースを始めましょう
Web 開発、プログラミング言語、ソフトウェア テスト、その他
並べ替えは、昇順または降順、アルファベット順または数値順、自然な方法、ランダムな順序、およびユーザー定義の順序で行うことができます。数値配列やインデックス付き配列などの配列、および連想配列の場合、並べ替えはキーに基づいて、または昇順または降順などの 2 つの順序のいずれかの値に基づいて昇順または降順配列で行われます。データ要素がソートされた形式である場合、配列でソートすると検索が容易になります。
並べ替えは、PHP でsort関数を使用して実行されます。さまざまなソート関数があります。
年功序列に基づいて家族のメンバーの年齢を知りたいとします。家族には 15 人のメンバーがいる場合もあります。 15 人のメンバーの年齢を並べ替えるには、sort 関数を使用し、結果をすぐに取得します。したがって、このような場合には、sort が登場し、推奨されます。
また、ライブラリを使用する必要はありません。
構文:
sort(array);
ここで、配列は入力配列の名前です。
次の例では、$people 配列と $ages 配列の要素を並べ替えます。
アルファベット順に並べ替えます:
$people = array ('Rama', 'James', 'Mary', 'Alice', 'Radha');
数値順に並べ替えます:
$ages = array (25,10,30,15,20);
上記の 2 つの配列を結合し、1 つの連想配列を作成します。
$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>"; }
出力:
したがって、単純な sort() の代わりに、asort() を使用します。 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 中国語 Web サイトの他の関連記事を参照してください。