Menyusun dalam PHP

WBOY
WBOYasal
2024-08-29 13:04:46447semak imbas

Isih ialah untuk menyusun elemen tatasusunan dalam susunan tertentu. PHP melakukan pengisihan pada tatasusunan biasa seperti tatasusunan angka dan tatasusunan bersekutu. Tatasusunan biasa seperti tatasusunan angka boleh diisih dengan menggunakan fungsi isih() ringkas dan untuk melaksanakan pengisihan pada tatasusunan bersekutu, kami mempunyai fungsi yang berbeza.

IKLAN Kursus Popular dalam kategori ini PEMBANGUN PHP - Pengkhususan | 8 Siri Kursus | 3 Ujian Olok-olok

Mulakan Kursus Pembangunan Perisian Percuma Anda

Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain

Pengisihan boleh dilakukan dalam susunan menaik atau menurun, susunan abjad atau berangka, cara semula jadi, rawak dan juga tertib yang ditentukan pengguna. Untuk tatasusunan seperti tatasusunan angka atau tatasusunan diindeks dan untuk tatasusunan bersekutu, pengisihan dilakukan dalam susunan menaik atau tatasusunan menurun berdasarkan kunci atau berdasarkan nilai dalam mana-mana dua susunan seperti tertib menaik atau menurun. Isih pada tatasusunan menjadikan carian anda lebih mudah jika elemen data berada dalam bentuk yang diisih.

Bagaimana Isih dilakukan dalam PHP?

Isih dilakukan dalam PHP menggunakan fungsi isihan. Terdapat pelbagai jenis fungsi.

Andaikan anda ingin mengetahui umur ahli keluarga berdasarkan kekananan. Mungkin terdapat 15 ahli dalam satu keluarga. Untuk mengisih umur 15 ahli, kami menggunakan fungsi isihan dan mendapatkan hasilnya dengan cepat. Oleh itu, dalam kes sebegini, isihan masuk ke dalam gambar dan lebih baik.

Selain itu, tidak perlu menggunakan mana-mana perpustakaan.

Sintaks:

sort(array);

di mana tatasusunan ialah nama tatasusunan input.

Contoh berikut mengisih elemen tatasusunan $people dan $ages.

Isih mengikut Susunan Abjad:

$people = array ('Rama', 'James', 'Mary', 'Alice', 'Radha');

Isih dalam Susunan Berangka:

$ages = array (25,10,30,15,20);

Menggabungkan dua tatasusunan di atas dan mencipta satu bersekutu.

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

Isih dalam Susunan Berangka dengan Contoh:

Kod:

<?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>";
} ?>

Output :

Menyusun dalam PHP

Isih dalam Susunan Abjad dengan Contoh:

Kod:

<?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>";
}
?>

Output :

Menyusun dalam PHP

Mengisih suara Asosiatif

Melakukan isihan pada tatasusunan bersekutu yang mempunyai perkaitan pasangan nilai kunci akan berakhir dengan kehilangan kunci. Walaupun pengisihan dilakukan, setiap elemen tatasusunan kini telah diberikan indeks angka baharu.

Kod:

// 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>";
}

Output:

Menyusun dalam PHP

Dan dengan itu, bukannya sort(), kami menggunakan sort(). asort() ialah fungsi yang menyusun elemen tatasusunan bersekutu dalam tertib menaik. Dan arsort() ialah fungsi yang menyusun elemen tatasusunan dalam tertib menurun. Kedua-duanya diisih mengikut nilai. Sekarang mari kita belajar tentang tatasusunan ini bersama-sama dengan fungsi tatasusunan lain secara terperinci

Jenis Isih dalam PHP

Jenis fungsi tatasusunan yang berbeza dinyatakan di bawah, bersama-sama dengan tertib isihan, sama ada dalam tertib menaik atau menurun dan fungsi mengisih sama ada mengikut kekunci atau isihan mengikut nilai turut disebut.

  • 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>";
}

Output:

Menyusun dalam 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>";
}

Output :

Menyusun dalam 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>";
}

Output:

Menyusun dalam 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>";
}

Output:

Menyusun dalam 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>";
}

Output:

Menyusun dalam 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>";
}

Output:

Menyusun dalam 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>";
}

Output:

Menyusun dalam 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>";
}
?>

Output :

Menyusun dalam 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>";
}

Output :

Menyusun dalam 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>";
}

Output :

Menyusun dalam 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>";
}

Output:

Menyusun dalam 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>";
}

Output :

Menyusun dalam 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>";
}

Output:

Menyusun dalam 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.

Atas ialah kandungan terperinci Menyusun dalam PHP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Pengesahan Borang PHPArtikel seterusnya:Pengesahan Borang PHP