PHP Sorting Arrays

PHP Sorting Arrays

PHP Sorting Arrays

Sorting arrays is a common operation in PHP, especially when dealing with large datasets. PHP provides multiple functions for sorting indexed and associative arrays in ascending or descending order.

1. Sorting Indexed Arrays

1.1. sort() - Sort an Indexed Array in Ascending Order

The sort() function sorts an indexed array in ascending order (smallest to largest or alphabetically A-Z).

Example:

$numbers = [4, 2, 8, 1, 3]; sort($numbers); print_r($numbers);

Output:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 8 )

1.2. rsort() - Sort an Indexed Array in Descending Order

The rsort() function sorts an indexed array in descending order (largest to smallest or alphabetically Z-A).

Example:

$numbers = [4, 2, 8, 1, 3]; rsort($numbers); print_r($numbers);

Output:

Array ( [0] => 8 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )

2. Sorting Associative Arrays

2.1. asort() - Sort Associative Array by Values (Ascending)

The asort() function sorts an associative array by values while maintaining the key-value pairs.

Example:

$ages = ["John" => 25, "Jane" => 30, "Bob" => 22]; asort($ages); print_r($ages);

Output:

Array ( [Bob] => 22 [John] => 25 [Jane] => 30 )
  • Bob (22) is the smallest value, so it comes first.

2.2. arsort() - Sort Associative Array by Values (Descending)

The arsort() function sorts an associative array by values in descending order.

Example:

$ages = ["John" => 25, "Jane" => 30, "Bob" => 22]; arsort($ages); print_r($ages);

Output:

Array ( [Jane] => 30 [John] => 25 [Bob] => 22 )
  • Jane (30) is the largest value, so it comes first.

2.3. ksort() - Sort Associative Array by Keys (Ascending)

The ksort() function sorts an associative array by keys in ascending order.

Example:

$ages = ["John" => 25, "Jane" => 30, "Bob" => 22]; ksort($ages); print_r($ages);

Output:

Array ( [Bob] => 22 [Jane] => 30 [John] => 25 )
  • Keys are sorted alphabetically (A-Z).

2.4. krsort() - Sort Associative Array by Keys (Descending)

The krsort() function sorts an associative array by keys in descending order.

Example:

$ages = ["John" => 25, "Jane" => 30, "Bob" => 22]; krsort($ages); print_r($ages);

Output:

Array ( [John] => 25 [Jane] => 30 [Bob] => 22 )
  • Keys are sorted alphabetically (Z-A).

3. Sorting Multidimensional Arrays

Sorting a multidimensional array requires using usort(), where you define a custom sorting function.

Example: Sorting an Array of People by Age

$people = [ ["name" => "John", "age" => 25], ["name" => "Jane", "age" => 30], ["name" => "Bob", "age" => 22] ]; usort($people, function ($a, $b) { return $a["age"] - $b["age"]; // Ascending order }); print_r($people);

Output:

Array ( [0] => Array ( [name] => Bob [age] => 22 ) [1] => Array ( [name] => John [age] => 25 ) [2] => Array ( [name] => Jane [age] => 30 ) )
  • The array is sorted by age in ascending order.

4. Case-Insensitive Sorting

PHP provides case-insensitive sorting for string values:

4.1. natcasesort() - Natural Order Case-Insensitive Sorting

$words = ["apple", "Orange", "banana", "grape"]; natcasesort($words); print_r($words);

Output:

Array ( [0] => apple [2] => banana [3] => grape [1] => Orange )
  • Sorts alphabetically without considering case sensitivity.

5. Custom Sorting with usort()

If you need a custom sorting function, use usort().

Example: Sorting Strings by Length

$words = ["apple", "banana", "cherry", "kiwi"]; usort($words, function ($a, $b) { return strlen($a) - strlen($b); // Sort by string length }); print_r($words);

Output:

Array ( [0] => kiwi [1] => apple [2] => banana [3] => cherry )
  • Shortest words come first.

6. Summary of PHP Sorting Functions

FunctionDescription
sort()Sorts an indexed array in ascending order (A-Z, 0-9).
rsort()Sorts an indexed array in descending order (Z-A, 9-0).
asort()Sorts an associative array by values in ascending order.
arsort()Sorts an associative array by values in descending order.
ksort()Sorts an associative array by keys in ascending order.
krsort()Sorts an associative array by keys in descending order.
usort()Custom sorting using a user-defined function.
natcasesort()Case-insensitive natural order sorting.

Conclusion

Sorting arrays in PHP is essential when working with large datasets. PHP provides built-in functions for simple sorting (like sort(), asort()), associative sorting (ksort(), arsort()), and custom sorting (usort()). Understanding these sorting methods will help you manage and process data efficiently in PHP applications.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close