PHP Sorting Arrays

PHP provides several functions to sort arrays. You can sort arrays by their values or keys, in ascending or descending order, and you can also use custom sorting functions.


Sort Functions For Arrays


  • sort() - sorts the array in ascending order
  • rsort() - sorts the array in descending order
  • asort() - sorts the array in ascending order while maintaining the key-value pairs
  • ksort() - sorts the array by key in ascending order
  • arsort() - sorts the array in descending order while maintaining the key-value pairs
  • krsort() - sorts the array by key in descending order


Sort Array in Ascending Order - sort()


The following example sorts the elements of the $fruitsarray in ascending alphabetical order:

Example
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = ["banana", "apple", "cherry", "date"];
sort($fruits); // Sorts in ascending alphabetical order
print_r($fruits);
?>
</body>
</html>

Try it yourself

The following example sorts the elements of the $numbers array in ascending numerical order:

Example
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = [4, 2, 8, 6];
sort($numbers); // Sorts in ascending order
print_r($numbers); // Output: [2, 4, 6, 8]
?>
</body>
</html>

Try it yourself

When using the sort() function in PHP to sort an array containing both numerical and alphabetical values, it sorts the elements lexicographically (alphabetically by character values).

Example
<!DOCTYPE html>
<html>
<body>
<?php
$array = ["apple", 10, "banana", 2, "cherry", 20];
sort($array); // Sorts the array in ascending order
print_r($array);
?>
</body>
</html>

Try it yourself

Sort Array in Descending Order - rsort()


The following example sorts the elements of the $fruitsarray in descending alphabetical order:


Example
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = ["banana", "apple", "cherry", "date"];
rsort($fruits); // Sorts in ascending alphabetical order
print_r($fruits);
?>
</body>
</html>

Try it yourself

The following example sorts the elements of the $numbers array in descending numerical order:

Example
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = [4, 2, 8, 6];
rsort($numbers); // Sorts in ascending order
print_r($numbers); // Output: [2, 4, 6, 8]
?>
</body>
</html>

Try it yourself

Sort Array according to Value (Ascending Order) - asort()


The following example sorts the array in ascending order while maintaining the key-value pairs.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$ages = ["John" => 30, "Alice" => 25, "Bob" => 35];
asort($ages); // Sorts by value in ascending order
print_r($ages); // Output: ["Alice" => 25, "John" => 30, "Bob" => 35]
?>
</body>
</html>

Try it yourself

Sort Array according to Value (Descending Order) - arsort()


The following example sorts the array in descending order while maintaining the key-value pairs.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$ages = ["John" => 30, "Alice" => 25, "Bob" => 35];
arsort($ages); // Sorts by value in descending order
print_r($ages); // Output: ["Bob" => 35, "John" => 30, "Alice" => 25]
?>
</body>
</html>

Try it yourself

Sort Array according to Key (Ascending Order) - ksort()


The following example sorts the array by key in ascending order while maintaining the key-value pairs.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$ages = ["John" => 30, "Alice" => 25, "Bob" => 35];
ksort($ages); // Sorts by key in ascending order
print_r($ages); // Output: ["Alice" => 25, "Bob" => 35, "John" => 30]
?>
</body>
</html>

Try it yourself

Sort Array according to Key (Descending Order) - krsort()


The following example sorts the array by key in descending order while maintaining the key-value pairs.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$ages = ["John" => 30, "Alice" => 25, "Bob" => 35];
krsort($ages); // Sorts by key in ascending order
print_r($ages); // Output: ["Alice" => 25, "Bob" => 35, "John" => 30]
?>
</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.