saftutorials.com
PHP Sorting Array Tutorial

There is some pre-built function for the array, by which we can sort the array values and index in descending or ascending order.

Kinds of Array
sort()
rsort()
asort()
arsort()
ksort()
krsort()

Example and function of sort()

sort() function sort the array in ascending order by its value. The PHP var_dump function return also datatypes

<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('banana', 'apple', 'mango');
var_dump($fruits);
$sorted_fruits = sort($fruits);
var_dump($sorted_fruits);
?>
</body>
</html>

The above code produces the following results

Outputs: array(3) { [0]=> string(5) 'banana' [1]=> string(6) 'apple' [2]=> string(5) 'mango' }

array(3) { [0]=> string(5) 'apple' [1]=> string(6) 'banana' [2]=> string(5) 'mango' }


Example and function of rsort()

rsort() function sort the array in descending order by its value. The PHP var_dump function return also datatypes


<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('banana', 'apple', 'mango');
var_dump($fruits);
$sorted_fruits = rsort($fruits);
var_dump($sorted_fruits);
?>
</body>
</html>

The above code produces the following results

Outputs: array(3) { [0]=> string(5) 'banana' [1]=> string(6) 'apple' [2]=> string(5) 'mango' }

array(3) { [0]=> string(5) 'mango' [1]=> string(6) 'banana' [2]=> string(5) 'apple' }


Example and function of asort()

asort() function sort the associative array in ascending order by its value. The PHP var_dump function return also datatypes

<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('red' => 'apple', 'yellow' => 'mango', 'green' =>  'banana');
var_dump($fruits);
$sorted_fruits = asort($fruits);
var_dump($sorted_fruits);
?>
</body>
</html>

The above code produces the following results

Outputs: array(3) { ['red']=> string(5) 'apple' ['yellow']=> string(5) 'mango' ['green']=> string(6) 'banana' }

array(3) { ['red']=> string(5) 'apple' ['green']=> string(6) 'banana' ['yellow']=> string(5) 'mango' }


Example and function of arsort()

arsort() function sort the associative array in descending order by its value. The PHP var_dump function return also datatypes

<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('red' => 'apple', 'yellow' => 'mango', 'green' =>  'banana');
var_dump($fruits);
$sorted_fruits = arsort($fruits);
var_dump($sorted_fruits);
?>
</body>
</html>

The above code produces the following results

Outputs: array(3) { ['red']=> string(5) 'apple' ['yellow']=> string(5) 'mango' ['green']=> string(6) 'banana' }

array(3) { ['yellow']=> string(5) 'mango' ['green']=> string(6) 'banana' ['red']=> string(5) 'apple' }


Example and function of ksort()

ksort() function sort the associative array in ascending order by its key. The PHP var_dump function return also datatypes


<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('red' => 'apple', 'yellow' => 'mango', 'green' =>  'banana');
var_dump($fruits);
$sorted_fruits = ksort($fruits);
var_dump($sorted_fruits);
?>
</body>
</html>

The above code produces the following results

Outputs: array(3) { ['red']=> string(5) 'apple' ['yellow']=> string(5) 'mango' ['green']=> string(6) 'banana' }

array(3) { ['green']=> string(6) 'banana' ['red']=> string(5) 'apple' ['yellow']=> string(5) 'mango' }


Example and function of krsort()

krsort() function sort the associative array in descending order by its key. The PHP var_dump function return also datatypes:

<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('red' => 'apple', 'yellow' => 'mango', 'green' =>  'banana');
var_dump($fruits);
$sorted_fruits = krsort($fruits);
var_dump($sorted_fruits);
?>
</body>
</html>

The above code produces the following results

Outputs: array(3) { ['red']=> string(5) 'apple' ['yellow']=> string(5) 'mango' ['green']=> string(6) 'banana' }

array(3) { ['yellow']=> string(5) 'mango' ['red']=> string(5) 'apple' ['green']=> string(6) 'banana' }


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic