Sortieren in PHP

WBOY
Freigeben: 2024-08-29 13:04:46
Original
306 Leute haben es durchsucht

Sortieren bedeutet, die Elemente eines Arrays in einer bestimmten Reihenfolge anzuordnen. PHP führt die Sortierung auf normalen Arrays wie einem numerischen Array und auf assoziativen Arrays durch. Normale Arrays wie numerische Arrays können mit der einfachen Funktion sort() sortiert werden. Um die Sortierung des assoziativen Arrays durchzuführen, stehen uns verschiedene Funktionen zur Verfügung.

WERBUNG Beliebter Kurs in dieser Kategorie PHP-ENTWICKLER - Spezialisierung | 8-Kurs-Reihe | 3 Probetests

Starten Sie Ihren kostenlosen Softwareentwicklungskurs

Webentwicklung, Programmiersprachen, Softwaretests und andere

Die Sortierung kann in aufsteigender oder absteigender Reihenfolge, alphabetischer oder numerischer Reihenfolge, natürlich, zufällig und auch in benutzerdefinierter Reihenfolge erfolgen. Bei Arrays wie dem numerischen Array oder dem indizierten Array und bei assoziativen Arrays erfolgt die Sortierung in aufsteigender oder absteigender Reihenfolge basierend auf dem Schlüssel oder basierend auf dem Wert in einer der beiden Reihenfolgen wie aufsteigend oder absteigend. Das Sortieren nach Arrays erleichtert die Suche, wenn die Datenelemente in sortierter Form vorliegen.

Wie wird die Sortierung in PHP durchgeführt?

Die Sortierung erfolgt in PHP mithilfe von Sortierfunktionen. Es gibt verschiedene Sortierfunktionen.

Angenommen, Sie möchten das Alter von Familienmitgliedern anhand des Dienstalters ermitteln. Eine Familie kann aus 15 Mitgliedern bestehen. Um das Alter von 15 Mitgliedern zu sortieren, verwenden wir die Sortierfunktion und erhalten schnell das Ergebnis. Daher kommt in einem solchen Fall die Sortierung ins Spiel und ist vorzuziehen.

Außerdem besteht keine Notwendigkeit, irgendwelche Bibliotheken zu verwenden.

Syntax:

sort(array);
Nach dem Login kopieren

wobei ein Array der Name des Eingabearrays ist.

Das folgende Beispiel sortiert die Elemente des Arrays $people und $ages.

In alphabetischer Reihenfolge sortieren:

$people = array ('Rama', 'James', 'Mary', 'Alice', 'Radha');
Nach dem Login kopieren

In numerischer Reihenfolge sortieren:

$ages = array (25,10,30,15,20);
Nach dem Login kopieren

Kombinieren Sie die beiden oben genannten Arrays und erstellen Sie ein assoziatives.

$people_ages = array ('James' => 25, 'Rama' => 10, 'Mary' => 30, 'Alice' => 15, 'Radha' => 20);
Nach dem Login kopieren

Sortieren Sie in numerischer Reihenfolge mit Beispiel:

Code:

<?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>";
} ?>
Nach dem Login kopieren

Ausgabe:

Sortieren in PHP

Sortieren Sie in alphabetischer Reihenfolge mit Beispiel:

Code:

<?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>";
}
?>
Nach dem Login kopieren

Ausgabe :

Sortieren in PHP

Sortieren eines assoziativen Arrays

Das Durchführen einer Sortierung für assoziative Arrays mit Schlüssel-Wert-Paar-Zuordnung führt zum Verlust der Schlüssel. Auch wenn die Sortierung durchgeführt wird, wurde nun jedem Element des Arrays ein neuer numerischer Index zugewiesen.

Code:

// 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>";
}
Nach dem Login kopieren

Ausgabe:

Sortieren in PHP

Und daher verwenden wir anstelle einer einfachen sort() asort(). asort() ist eine Funktion, die die Elemente eines assoziativen Arrays in aufsteigender Reihenfolge sortiert. Und arsort() ist eine Funktion, die die Elemente eines Arrays in absteigender Reihenfolge sortiert. Beide sind nach Wert sortiert. Lassen Sie uns nun dieses Array und andere Array-Funktionen im Detail kennenlernen

Arten der Sortierung in PHP

Die verschiedenen Arten von Array-Funktionen werden unten erwähnt, zusammen mit der Sortierreihenfolge, ob aufsteigend oder absteigend, und die Funktion sortiert entweder nach Schlüssel oder nach Wert.

  • sort(): this function sorts the input array in ascending order and sorts it by value
  • rsort(): this function sorts the input array in descending order and sorts it by value
  • asort(): this function sorts the input array in ascending order and sorts it by value
  • arsort(): this function sorts the input array in descending order and sorts by value
  • ksort(): this function sorts the input array in ascending order and sorts it by key
  • krsort(): this function sorts the input array in descending order and sorts it by key
  • usort(): this function sorts the input array based on the user-defined function and sorts by value
  • uasort(): this function sorts the input array based on the user-defined function and sorts by value
  • uksort(): this function sorts the input array based on the user-defined function and sorts by key
  • natsort(): this function sorts the input array-based natural ordering.
  • natcasesort(): this function sorts the input array-based natural ordering and is case insensitive.
  • shuffle(): this function sorts the input array based on the value, and the output is a random order of values.

Let us learn about each function in detail

1. sort()

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>";
}
Nach dem Login kopieren

Output:

Sortieren in PHP

2. rsort()

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>";
}
Nach dem Login kopieren

Output :

Sortieren in PHP

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>";
}
Nach dem Login kopieren

Output:

Sortieren in PHP

3. asort()

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>";
}
Nach dem Login kopieren

Output:

Sortieren in PHP

4. arsort()

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>";
}
Nach dem Login kopieren

Output:

Sortieren in PHP

5. ksort()

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>";
}
Nach dem Login kopieren

Output:

Sortieren in PHP

6. krsort()

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>";
}
Nach dem Login kopieren

Output:

Sortieren in PHP

7. natsort()

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>";
}
?>
Nach dem Login kopieren

Output :

Sortieren in PHP

8. natcasesort()

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>";
}
Nach dem Login kopieren

Output :

Sortieren in PHP

9. usort()

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>";
}
Nach dem Login kopieren

Output :

Sortieren in PHP

10. uasort()

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>";
}
Nach dem Login kopieren

Output:

Sortieren in PHP

11. uksort()

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>";
}
Nach dem Login kopieren

Output :

Sortieren in PHP

12. shuffle()

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>";
}
Nach dem Login kopieren

Output:

Sortieren in PHP

Conclusion

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.

Das obige ist der detaillierte Inhalt vonSortieren in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
php
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!