Sorting arrays in PHP

Walden Systems, Geek corner, ad, authenticate, script, geeks corner, json, image, picture, server, file, write, read, close, fread, readfile, fclose, walden, systems, walden systems, php, functions, variable arguments, loops,  for loop, loop, while, do while, break, continue, decision making, ternary, operator, if, then, else
PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

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. For a description of what an array is, click here.

Sort indexed arrays

The sort() function is used for sorting the elements of the indexed array in ascending order, alphabetically for letters and numerically for numbers. This is similar to the list.sort() in Python. The rsort() function is used for sorting the elements of the indexed array in descending order, again, alphabetically for letters and numerically for numbers. To do this in Python, we would use the sort() function with the inclusion of the reverse=TRUE parameter.

<?php
1   $numbers = array(1, 2, 6, 4, 7, 10);
2 
3   sort($numbers);
4   print_r($numbers);
5   rsort($numbers) ;
6   print_r($numbers) ;
?>


Line 3 will sort in ascending order and Line 5 will sort in descending order


Sort associative arrays by value

The asort() function sorts the elements of an associative array in ascending order according to the value. It works just like sort(), but it preserves the association between keys and its values while sorting. This is similar to Python's sorted(ARRAY.value) method. The arsort() function sorts the elements of an associative array in descending order according to the value. It works just like rsort(), but it preserves the association between keys and its values while sorting. In Python, we would use the sorted(ARRAY.value) method with the reverse=TRUE parameter.

<?php
1    $age = array("John"=>20, "Jane"=>19, "David"=>45, "Martha"=>35);
2 
3    asort($age);
4    print_r($age);
5    arsort($age);
6    print_r($age);
?>


Line 4 will have the following output:
Array ( [Jane] => 19 [John] => 20 [Martha] => 35 [David] => 45 )

Line 6 will have the following output:
Array ( [David] => 45 [Martha] => 35 [John] => 20 [Jane] => 19 )

Sorting associative array by key

The ksort() function sorts the elements of an associative array in ascending order by their keys. It preserves the association between keys and its values while sorting, same as asort() function. In Python, we would use the sorted(ARRAY) method. The krsort() function sorts the elements of an associative array in descending order by their keys. It preserves the association between keys and its values while sorting, same as arsort() function. In Python, we would use the sorted(ARRAY) with reverse=TRUE parameter.

<?php
1    $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
2
3    ksort($age);
4    print_r($age);
5
6    krsort($age);
7    print_r($age);
?>


Line 4 will have the following output:
Array ( [David] => 45 [Jane] => 19 [John] => 20 [Martha] => 35 )

Line 6 will have the following output:
Array ( [Martha] => 35 [John] => 20 [Jane] => 19 [David] => 45 )