Search

PHP sorting array

PHP
post-title

Today we will see how you can sort array in different order. We will use all methods with example. So first lets start with simple sorting method.

sort() method

sort() method simply sort the array in alphabetical ascending order by value.

<?php

$colors = array('red', 'orange', 'yellow');

sort($colors);

print_r($colors);
// Array ( [0] => orange [1] => red [2] => yellow )

rsort() method

rsort() method sort the array in alphabetical descending order, by its value.

<?php

$colors = array('red', 'orange', 'yellow');

rsort($colors);

print_r($colors);
// Array ( [0] => yellow [1] => red [2] => orange )

asort() method

asort() method sort associative array in ascending order, by its value.

<?php

$colors = array('apple' => 'red', 'mango' => 'orange', 'banana' => 'yellow');

asort($colors);

print_r($colors);
Array ( [mango] => orange [apple] => red [banana] => yellow )

arsort() method

arsort() method sorts associative array in descending order, according to the value.

<?php

$colors = array('apple' => 'red', 'mango' => 'orange', 'banana' => 'yellow');

arsort($colors);

print_r($colors);
// Array ( [banana] => yellow [apple] => red [mango] => orange )

natsort() method

natsort() method sorts array in natural order, according to the value. The method uses a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations.

See below example between sort() method result and natsort() method result:

<?php

$lists = array('list1', 'list10', 'list5', 'list12');

natsort($lists);

print_r($lists);
// Array ( [0] => list1 [1] => list5 [2] => list10 [3] => list12 )

sort($lists);

print_r($lists);
// Array ( [0] => list1 [1] => list10 [2] => list12 [3] => list5 )

natcasesort() method

natcasesort() method sorts array in natural order without taking ca-sensitive into consideration, according to the value. natcasesort() method is same as natsort() except it is a case insensitive. Now let's see example below so we can know exact difference between them.

<?php

$lists = array('list1', 'List10', 'list5', 'list12');

natcasesort($lists);
print_r($lists);
// Array ( [0] => list1 [2] => list5 [1] => List10 [3] => list12 )

natsort($lists);
print_r($lists);
// Array ( [1] => List10 [0] => list1 [2] => list5 [3] => list12 )

ksort() method

ksort() method sorts associative array in ascending order, according to the key.

<?php

$colors = array('apple' => 'red', 'mango' => 'orange', 'banana' => 'yellow');

ksort($colors);

print_r($colors);
// Array ( [apple] => red [banana] => yellow [mango] => orange )

krsort() method

krsort() method sorts associative array in descending order, according to the key.

<?php

$colors = array('apple' => 'red', 'mango' => 'orange', 'banana' => 'yellow');

krsort($colors);

print_r($colors);
// Array ( [mango] => orange [banana] => yellow [apple] => red )

So these are methods to sort array in PHP. You can use them as per your requirements.