0% found this document useful (0 votes)
4 views

Php Arrays Sorting

Uploaded by

Manisha Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Php Arrays Sorting

Uploaded by

Manisha Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Php Arrays Sorting

Chapter 8
Presentation By

 Name : Manisha Rathod


 Roll No : 4
What is sorting?

 Sorting refers to ordering data in an alphabetical order or numerical


order, descending order(high to low) or ascending order(low to high).
 Sorting greatly improves the efficiency of searching.
PHP Functions For Sorting
Arrays
 PHP comes with a number of built-in functions designed specifically
for sorting array elements in different ways like alphabetically or
numerically in ascending or descending order. Here we'll explore
some of these functions most commonly used for sorting arrays
Sort() : Sort Arrays in Ascending
Order
The following example sorts the elements of the $mobile array in
ascending alphabetical order:
<?php
$mobile = array("Samsung", "OnePlus", "Apple");
sort($mobile);
$mlength = count($mobile);
for($x = 0; $x < $mlength; $x++) {
echo $mobile[$x];
echo "<br>";
}
?>
Output :

 Apple
OnePlus
Samsung
rsort() : Sort Array in Descending Order

 The following example sorts the elements of the $subject array in


descending alphabetical order:
 <?php
 $subject = array("Computer", "Science", "Maths");
 rsort($subject);
 $slength = count($subject);
 for($x = 0; $x < $slength; $x++) {
 echo $subject[$x];
 echo "<br>";
 }
 ?>
Output :

 Science
Maths
Computer
Asort : Sort Array (Ascending Order),
According to Value

 The following example sorts an associative array in ascending


order, according to the value:
 <?php
 $price = array("TV"=>"6000", "Mobile"=>"5000",
"Headphone"=>"700");
 asort($price);
 foreach($price as $x => $x_value) {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
 }
 ?>
Output :

 Key=Headphone, Value=700
Key=Mobile, Value=5000
Key=TV, Value=6000
ksort : Sort Array (Ascending Order),
According to Key

 The following example sorts an associative array in ascending


order, according to the key:
 <?php
 $price = array("TV"=>"6000", "Mobile"=>"5000",
"Headphone"=>"7000");
 ksort($price);
 foreach($price as $x => $x_value) {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
 }
 ?>
Output :

 Key=Headphone, Value=7000
Key=Mobile, Value=5000
Key=TV, Value=6000
arsort() : Sort Array (Descending Order),
According to Value

 The following example sorts an associative array in descending


order, according to the value:
 <?php
 $price = array("TV"=>"6000", "Mobile"=>"5000",
"Headphone"=>"700");
 arsort($price);
 foreach($price as $x => $x_value) {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
 }
 ?>
Output :

 Key=TV, Value=6000
Key=Mobile, Value=5000
Key=Headphone, Value=700
krsort() : Sort Array (Descending Order),
According to Key

 The following example sorts an associative array in descending


order, according to the key:
 <?php
 $price = array("TV"=>"6000", "Mobile"=>"5000",
"Headphone"=>"700");
 krsort($price);
 foreach($price as $x => $x_value) {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
 }
 ?>
Output :

 Key=TV, Value=6000
Key=Mobile, Value=5000
Key=Headphone, Value=700
BIBLIOGRAPHY AND REFERENCES

 https://www.google.com/

 https://www.tutorialspoint.com/

 https://www.w3schools.com/
Thank
You!

You might also like