PHP Sort Array

PHP Sort array is a basic concept of any programming language. Access and Manipulate Data from any Data Structure, such as an Array or Array, without Sorting, is quite time consuming. As a result, the speed of the program is greatly reduced. Therefore it is very important to sort out the Data Structure’s data.

PHP provides different types of functions for sorting different elements of an Array. Generally, all elements of Array are sorted based on the English language. But if we do data store in any other language in Array and want to sort the Store data in this other language, then we set that Primary Language by using the setlocale () Function to perform the Sorting Operation Perform, In which the data are Stored.

sort() – Sort Array in Ascending Order

This Function works to sort the values ​​of an Array in Ascending Order. In this Function, we first need to specify the Array as Argument, which we want to sort. While in the second argument, we can change the Sorting Behavior of this Function by specifying one of the following Flags. php sort array

Example

<?php
$studentName = array("Neha","Sumit","Karam","Manika");
sort($studentName);

$arraylength = count($studentName);
for($a = 0; $a < $arraylength; $a++) {
    echo $studentName[$a];
    echo "<br>";
}
?>

Output

Karam
Manika
Neha
Sumit

Sorting the values of Array is used when using the sort () Function, but Index Number is maintained. That is, Index Number 0 can move to Index Number 4 after Placed Value Sorting, as is being done in the first example program.

Example

<?php
$rollNumber = array(8,6,10,5,3);
sort($rollNumber);

$arraylength = count($rollNumber);
for($a = 0; $a < $arraylength; $a++) {
    echo $rollNumber[$a] . " ";
}
?>

Output

3 5 6 8 10

rsort() – Sort Array in Descending Order

When we want to do Array in Descending Order instead of Sorting Ascending Order, then we use the rsort () function instead of the sort () Function. This Function works the same way, like the Function does the Function and it also accepts arguments just like the fine sort function. The difference is only in the result of returning to them. php sort array

Example

<?php
$studentName = array("Neha","Sumit","Karam","Manika");
rsort($studentName);

$arraylength = count($studentName);
for($a = 0; $a < $arraylength; $a++) {
    echo $studentName[$a];
    echo "<br>";
}
?>

Output

Sumit
Neha
Manika
Karam

Example

<?php
$rollNumber = array(8,6,10,5,3);
rsort($rollNumber);

$arraylength = count($rollNumber);
for($a = 0; $a < $arraylength; $a++) {
    echo $rollNumber[$a] . " ";
}
?>

Output

10 8 6 5 3

asort() – Sort Array (Ascending Order), According to Value

This function is similar to the sort () function and sorts an Array in the Ascending Order, but if you use this function, Array’s Key => Value Pair Maintain remains. To understand this, we see an example. php sort array

Example

<?php
$rollNumber = array("Karam"=>"4", "Sumit"=>"2", "Radha"=>"1", "Rohit"=>"3");
asort($rollNumber);

foreach($rollNumber as $name => $number) {
    echo "Student Name= " . $name . ", Roll Number=" . $number;
    echo "<br>";
}
?>

Output

Student Name= Radha ,Roll Number= 1
Student Name= Sumit ,Roll Number= 2
Student Name= Rohit ,Roll Number= 3
Student Name= Karam ,Roll Number= 4

arsort() – Sort Array (Descending Order), According to Value

This Function works just like the Exactly asort () Function. The only difference is that it sorts the values ​​of the Function Array into Descending Order instead of the Ascending Order. php sort array

Example

<?php
$rollNumber = array("Karam"=>"4", "Sumit"=>"2", "Radha"=>"1", "Rohit"=>"3");
arsort($rollNumber);

foreach($rollNumber as $name => $number) {
    echo "Student Name= " . $name . ", Roll Number=" . $number;
    echo "<br>";
}
?>

Output

Student Name= Karam ,Roll Number= 4
Student Name= Rohit ,Roll Number= 3
Student Name= Sumit ,Roll Number= 2
Student Name= Radha ,Roll Number= 1

ksort() – (Ascending Order), According to Key

Using this function, we can sort the values ​​of an Array, but not its values. If the Function successfully sorts the keys of an Array, then this Function returns True Return, otherwise False Returns. php sort array

Example

<?php
$rollNumber = array("Karam"=>"4", "Sumit"=>"2", "Amit"=>"1", "Dharam"=>"3");
ksort($rollNumber);

foreach($rollNumber as $name => $number) {
    echo "Student Name= " . $name . ", Roll Number=" . $number;
    echo "<br>";
}
?>

Output

Student Name= Amit ,Roll Number= 1
Student Name= Dharam ,Roll Number= 3
Student Name= Karam ,Roll Number= 4
Student Name= Sumit ,Roll Number= 2

krsort() – (Descending Order), According to Key

This Function Exactly works just like the ksort () Function. The only difference is that it sorts the Funse Array Keys into Descending Order instead of Ascending Order. php sort array

Example

<?php
$rollNumber = array("Karam"=>"4", "Sumit"=>"2", "Amit"=>"1", "Dharam"=>"3");
krsort($rollNumber);

foreach($rollNumber as $name => $number) {
    echo "Student Name= " . $name . ", Roll Number=" . $number;
    echo "<br>";
}
?>

Output

Student Name= Sumit ,Roll Number= 2
Student Name= Karam ,Roll Number= 4
Student Name= Dharam ,Roll Number= 3
Student Name= Amit ,Roll Number= 1
php sort array