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 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 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 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 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 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 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 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 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.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
React Responsive Animated Navbar Example
In this article, I will share with you h...Syntax error or access violation: 1055 'posts.views' isn't in GROUP BY
While working with Laravel database quer...HTML To PDF In Laravel Using barryvdh/laravel-snappy
Today, laravel code share with you how t...How do I ask for Location Permissions in Javascript
In this article, we will share with you...How to create and customise login in laravel
Laravel has great features out of the bo...