PHP HOME
PHP Introduction
PHP SETUP
PHP Syntax
PHP Data Types
PHP Outputs
PHP Variable
PHP Constant
PHP Strings
PHP Operators
PHP if else if
PHP Switch
PHP while loop
PHP for loop
PHP Arrays
PHP Functions
PHP Sorting Array
PHP Form Handling
PHP Form Validation
PHP Form Required
PHP Form URL/EMAIL
PHP Form Submit
PHP Contact Form
PHP DATE & TIME
PHP INCLUDE
PHP FILE HANDLING
PHP FILE OPEN/READ
PHP FILE CREATE/WRITE
PHP FILE UPLOAD
PHP SESSION
PHP COOKIES
PHP FILTER
PHP ERROR HANDLING
PHP EXCEPTION
PHP Redirects
PHP with MySQL
PHP WITH MYSQL DB
MYSQL CONNECT
MYSQL CREATE DATABASE
SELECT DATABASE
MYSQL CREATE TABLE
MYSQL INSERT DATA
MYSQL SELECT DATA
MYSQL UPDATE DATA
MYSQL DELETE DATA
MYSQL ORDER
MYSQL LIMIT
MYSQL LAST ID
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()
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' }
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' }
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' }
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' }
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' }
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' }