php sort array – Best 4 Ways To Sort Arrays in PHP?

Today, We want to share with you php sort array.In this post we will show you php sort multidimensional array, hear for PHP sort associative array by value we will give you demo and example for implement.In this post, we will learn about PHP Sorting Arrays Tutorial with an example.

Free Live Chat for Any Issue

php sort array

  • sort()
  • rsort()
  • asort()
  • ksort()
  • arsort()
  • krsort()

Example 1 : PHP Sorting Arrays

<!DOCTYPE html>
<html>
<body>

<?php
$members = array("Mayur", "Hitesh", "Dhaval");
sort($members);

$clength = count($members);
for($x = 0; $x < $clength; $x++) {
  echo $members[$x];
  echo "<br>";
}
?>

</body>
</html>

Example 2 : Sorting Indexed Arrays in Ascending Order

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Sorting PHP Indexed Array in Ascending Order</title>
</head>
<body>

<?php
// Define array
$members = array("Raj", "Gori", "Bhuro", "yash");
 
// Sorting and printing array
sort($members);
var_dump($members);
?>

</body>
</html>

Example 3 : Sorting Indexed Arrays in Descending Order

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Sorting PHP Indexed Array in Descending Order</title>
</head>
<body>

<?php
// Define array
$members = array("Ravi", "Gaurav", "Bhavik", "Yashu");
 
// Sorting and printing array
rsort($members);
var_dump($members);
?>

</body>
</html>

Example 4 : Sorting Associative Arrays in Ascending Order By Value

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Sorting PHP Associative Array in Ascending Order by Value</title>
</head>
<body>

<?php
// Define array
$members = array("Virat"=>20, "Hallish"=>14, "Jagruti"=>45, "chandni"=>35);
 
// Sorting array by value and print
asort($members);
var_dump($members);
?>

</body>
</html>

Download

I hope you get an idea about php sort array.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.