Welcome to all,
Today we practise how to sort an array using php. We know this same problem solving using C, CPP or JAVA etc. But here I share how to solve this problem in php.
To arrange the elements in an array in numerical order from highest to lowest values (descending order) or lowest to highest value (ascending order). In php, the inbuilt functions are available for performing sorting operations.They are:
- sort() – sort arrays in ascending order
- rsort() – sort arrays in descending order
- asort() – sort associative arrays in ascending order, according to the value
- ksort() – sort associative arrays in ascending order, according to the key
- arsort() – sort associative arrays in descending order, according to the value
- krsort() – sort associative arrays in descending order, according to the key
Sort Array in Ascending Order – sort()
The code below sorts array elements of the $test in ascending alphabetical order.
"; } ?>
Now array elements will go in the alphabetical order. Output will be the following:
Delhi
Kerala
Tamilnadu
We can sort values by numerical order too. The code below sorts array elements of the $test in ascending numerical order.
"; } ?>
Now array elements will go in the numerical order. Output will be the following:
0.01
0.567
19.45465
70
Sort Array in Descending Order – rsort()
The code below sorts array elements of the $test in descending alphabetical order.
"; } ?>
Now array elements will go in the alphabetical order. Output will be the following:
Tamilnadu
Kerala
Delhi
We can sort values by numerical order too. The code below sorts array elements of the $test in descending numerical order.
"; } ?>
Now array elements will go in the numerical order. Output will be the following:
70
19.45465
0.567
0.01
Sort associative arrays in ascending order, according to the value- asort()
The code below sorts an associative array in ascending order based on values.
"39", "BHJHG"=>"37", "AAE"=>"43"); asort($test); foreach($test as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?>
The output will be the following:
Key=BHJHG, Value=37
Key=ABCS, Value=39
Key=AAE, Value=43
Sort associative arrays in ascending order, according to the key- ksort()
The code below sorts an associative array in ascending order based on key.
"39", "BHJHG"=>"37", "AAE"=>"43"); ksort($test); foreach($test as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?>
The output will be the following:
Key=AAE, Value=43
Key=ABCS, Value=39
Key=BHJHG, Value=37
Sort associative array in descending order, according to the value – arsort()
The code below sorts an associative array in descending order based on values.
"39", "BHJHG"=>"37", "AAE"=>"43"); arsort($test); foreach($test as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?>
The output will be the following:
Key=AAE, Value=43
Key=ABCS, Value=39
Key=BHJHG, Value=37
Sort associative arrays in descending order, according to the key – krsort()
The code below sorts an associative array in descending order based on key.
"39", "BHJHG"=>"37", "AAE"=>"43"); krsort($test); foreach($test as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?>
The output will be the following:
Key=BHJHG, Value=37
Key=ABCS, Value=39
Key=AAE, Value=43
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.