List of functions available to sort an PHP array? Explain with example?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

In PHP, we have six functions for sorting array:

  • sort()= it sort an array in ascending order
  • rsort()= it sort an array in descending order
  • asort()= it sort an array in ascending order, according to the value
  • ksort()= it sort an array in ascending order, according to the key
  • arsort()= it sort an array in descending order, according to the value
  • krsort()= it sort an array in descending order, according to the key

example

<?php
		$num = array(21,23,34,45,1,4,5,);
		 sort($num);

	   $numlength = count($num);
		 for($t = 0; $t < $numlength; $t++) 
		 {
		    echo $num[$t];
		   
		 }
		 
		 
		 echo "<br><br>";
		 echo "<br>";
		 
		 
		 
		 $num = array(21,23,34,45,1,4,5,);
		 rsort($num);

	   $numlength = count($num);
		 for($t = 0; $t < $numlength; $t++) 
		 {
		    echo $num[$t];
			echo "<br>";
		   
		 }
		 
		 echo "<br>";
		 echo "<br>";
		 
		 $team = array("shivam"=>"20", "ravi"=>"22", "abhi"=>"24");
         asort($team);

         foreach($team as $value => $value_v) 
		 {
         echo "Key=" . $value . ", Value=" . $value_v;
         echo "<br>";
         }

          echo "<br>";
		 echo "<br>";
		 
		 $team = array("shivam"=>"20", "ravi"=>"22", "abhi"=>"24");
         ksort($team);

         foreach($team as $value => $value_v) 
		 {
         echo "Key=" . $value . ", Value=" . $value_v;
         echo "<br>";
		 }
		 
		 
		 echo "<br>";
		 echo "<br>";
		 
		 $team = array("shivam"=>"20", "ravi"=>"22", "abhi"=>"24");
         arsort($team);

         foreach($team as $value => $value_v) 
		 {
         echo "Key=" . $value . ", Value=" . $value_v;
         echo "<br>";
		 }

		 
		 
		 
		 echo "<br>";
		 echo "<br>";
		 
		 $team = array("shivam"=>"20", "ravi"=>"22", "abhi"=>"24");
         krsort($team);

         foreach($team as $value => $value_v) 
		 {
         echo "Key=" . $value . ", Value=" . $value_v;
         echo "<br>";
		 }


?>

output

14521233445


45
34
23
21
5
4
1


Key=shivam, Value=20
Key=ravi, Value=22
Key=abhi, Value=24


Key=abhi, Value=24
Key=ravi, Value=22
Key=shivam, Value=20


Key=abhi, Value=24
Key=ravi, Value=22
Key=shivam, Value=20


Key=shivam, Value=20
Key=ravi, Value=22
Key=abhi, Value=24

Related Posts

Modernizing Software Delivery in the Dutch Tech Landscape: A Pragmatic DevOps Playbook

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Finding Qualified AWS DevOps Trainers: Enterprise Playbook

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Teaching Failure: How to Build Incident Response and Resilience into DevOps Training

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

The Open Road in Japan: An Independent Rider’s Handbook for Cross-Country Touring

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

A Comprehensive Guide to Scooters, Motorcycles, and Road Trips

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Monitoring and Observability Training: Complete Curriculum Guide

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
guest

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x