Site icon T4Tutorials.com

PHP Sort Array

PHP Sort Array

In this tutorial, we will try to learn the sorting an array in PHP.

The sort() Function

The sort function sorts the arrays in ascending order (e.g; 1,2,3,4,…).

<?php
$subjects = array("C", "DB", "OOP");
sort($subjects);
?>

Output

C
DB
OOP

The arsort() Function

The arsort function sorts the associative arrays in descending order (e.g; 4,3,2,1), according to the value.

<?php
$subjects = array("C", "DB", "OOP");
sort($subjects);
?>

Output

OOP
DB
C

The ksort() Function

The ksort function sorts the associative arrays in ascending order (e.g; 1,2,3,4,…), according to the key.

The rsort() Function

The rsort function sorts the arrays in descending order (e.g; 4,3,2,1).

<!DOCTYPE html>
<html>
<body>

<?php
$subjects = array("C", "DB", "OOP");
rsort($subjects);

$subjectsLength = count($subjects);
for($x = 0; $x < $subjectsLength; $x++) {
    echo $subjects[$x];
    echo "<br>";
}
?>
</body>
</html>

Output

OOP
DB
C

The asort()

The asort function sorts the associative arrays in ascending order (e.g; 1,2,3,4,…).

<!DOCTYPE html>
<html>
<body>

<?php
$marks = array("Ali"=>"22", "Akram"=>"55", "Asad"=>"5", 
Sameed"=>"90" );
asort($marks);

foreach($marks as $Vary => $Vary_value) {
    echo "Key=" . $Vary . ", Value=" . $Vary_value;
    echo "<br>";
}
?>

</body>
</html>

Output

Key=Asad, Value=5
Key=Ali, Value=22
Key=Akram, Value=55
Key=Sameed, Value=90

Attributes of Sorting Functions

Some attributes of sorting the functions are mentioned below.

ksort() Maintains key association Sorting with Key
natcasesort() Maintains key association Sorting with Values
asort() Maintains key association Sorting with Values
sort() Does not Maintains key association Sorting with Values
uasort() Maintains key association Sorting with Values
uksort() Maintains key association Sorting with Key
krsort() Maintains key association Sorting with Key
natsort() Maintains key association Sorting with Values
rsort() Does not Maintains key association Sorting with Values
shuffle() Does not Maintains key association Sorting with Values
usort() Does not Maintains key association Sorting with Values
array_multisort() associative Maintains key association, numeric Does not Maintain key association Sorting with Values
arsort() Maintains key association Sorting with Values

Sort an array in PHP

Sorting means to arrange the array values in an ascending or descending order. Here, we are sharing the code of Sort an array in PHP.

<?php
$array_a=array(25,10,58,62,58);
$array_b=array(11,27,26,35,87);
$array=array(10);
for ($i=0;$i<5;$i++)
$array[$i]=$array_a[$i];
for ($i=5;$i<10;$i++)
$array[$i]=$array_b[$i-5];
for ($i=0;$i<9;$i++)
{
for ($j=0;$j<10-$i-1;$j++)
if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
}
echo 'The First array <br>';
for ($i=0;$i<5;$i++)
echo $array_a[$i]. " ";
echo '<br> The second array <br>';
for ($i=0;$i<5;$i++)
echo $array_b[$i]." ";
echo '<br> The union of sorted array <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]." ";
?>

Output

The First array
25 10 58 62 58
The second array
11 27 26 35 87
The union of the sorted array
10 11 25 26 27 35 58 58 62 87

Sort an array in PHP using Functions

Here, we are sharing the code of Sort an array in PHP using functions.

<?php
$array=array(25,10,58,62,58,11,27,26,35,87);
echo '<br> The array <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]." ";
function arrsort(){
$array=array(25,10,58,62,58,11,27,26,35,87);
for ($i=0;$i<9;$i++)
{
for ($j=0;$j<10-$i-1;$j++){
if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
}
}
echo '<br> The sorted array <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]." ";
}
arrsort();
?>

Output

The array
25 10 58 62 58 11 27 26 35 87
The sorted array
10 11 25 26 27 35 58 58 62 87

Sort an array in PHP with While Loop

Here, we are sharing the code of Sort an array in PHP with while loop.

<?php

function sorting(){
$array_a=array(25,10,58,62,58);
$array_b=array(11,27,26,35,87);
$array=array(10);
$i=0;
while ($i<5){
$array[$i]=$array_a[$i];
$i++;
}
$i=5;
while($i<10){
$array[$i]=$array_b[$i-5];
$i++;}
$i=0;
while($i<9){
$j=0;
while($j<10-$i-1){
if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
$j++;
}
$i++;
}
echo 'The First array <br>';
$i=0;
while($i<5)
{
echo $array_a[$i]. " ";
$i++;}
echo '<br> The second array <br>';
$i=0;
while($i<5){
echo $array_b[$i]." ";
$i++;}
echo '<br> The union of sorted array <br>';
$i=0;
while($i<10)
{
echo $array[$i]." ";
$i++;}
}
sorting();
?>

Output

The First array
25 10 58 62 58
The second array
11 27 26 35 87
The union of the sorted array
10 11 25 26 27 35 58 58 62 87

Sort an array in PHP with While Loop using functions

Here, we are sharing the code of Sort an array in PHP with while loop using functions.

<?php
$array_a=array(25,10,58,62,58);
$array_b=array(11,27,26,35,87);
$array=array(10);
$i=0;
while ($i<5){
$array[$i]=$array_a[$i];
$i++;
}
$i=5;
while($i<10){
$array[$i]=$array_b[$i-5];
$i++;}
$i=0;
while($i<9){
$j=0;
while($j<10-$i-1){
if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
$j++;
}
$i++;
}
echo 'The First array <br>';
$i=0;
while($i<5)
{
echo $array_a[$i]. " ";
$i++;}
echo '<br> The second array <br>';
$i=0;
while($i<5){
echo $array_b[$i]." ";
$i++;}
echo '<br> The union of sorted array <br>';
$i=0;
while($i<10)
{
echo $array[$i]." ";
$i++;}
?>

Output

The First array
25 10 58 62 58
The second array
11 27 26 35 87
The union of the sorted array
10 11 25 26 27 35 58 58 62 87

Sort an Array in PHP with Form Values Entered By The User

Here, we are sharing the code of Sort an array in PHP with the form values entered by the user.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; 
charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form  method="post"> 
First Array <br>
Enter Value: <input type="text" name="1"><br>
Enter Value: <input type="text" name="2"><br>
Enter Value: <input type="text" name="3"><br>
Enter Value: <input type="text" name="4"><br>
Enter Value: <input type="text" name="5"><br>
Second Array <br>
Enter Value: <input type="text" name="6"><br>
Enter Value: <input type="text" name="7"><br>
Enter Value: <input type="text" name="8"><br>
Enter Value: <input type="text" name="9"><br>
Enter Value: <input type="text" name="10"><br>
<input type="submit"value="Union of Array" name="Sort">
</form>
<?php
if (isset($_POST['Sort'])){
$array_a=array(5);
$array_b=array(5);
$array=array(10);
$array_a[0]=$_POST['1'];
$array_a[1]=$_POST['2'];
$array_a[2]=$_POST['3'];
$array_a[3]=$_POST['4'];
$array_a[4]=$_POST['5'];
$array_b[0]=$_POST['6'];
$array_b[1]=$_POST['7'];
$array_b[2]=$_POST['8'];
$array_b[3]=$_POST['9'];
$array_b[4]=$_POST['10'];
for ($i=0;$i<5;$i++)
$array[$i]=$array_a[$i];
for ($i=5;$i<10;$i++)
$array[$i]=$array_b[$i-5];
for ($i=0;$i<9;$i++)
{
for ($j=0;$j<10-$i-1;$j++)
if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
}
echo 'The First array <br>';
for ($i=0;$i<5;$i++)
echo $array_a[$i]. " ";
echo '<br> The second array <br>';
for ($i=0;$i<5;$i++)
echo $array_b[$i]." ";
echo '<br> The union of sorted array <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]." ";
}
?>
</body>
</html>
Output
Figure: How to Sort an array in PHP

Sort an Array in PHP with Form Values Entered By The User using functions

Here, we are sharing the code of Sort an array in PHP with the form values entered by the user using functions.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; 
charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form  method="post"> 
Enter Value: <input type="text" name="1"><br>
Enter Value: <input type="text" name="2"><br>
Enter Value: <input type="text" name="3"><br>
Enter Value: <input type="text" name="4"><br>
Enter Value: <input type="text" name="5"><br>

Enter Value: <input type="text" name="6"><br>
Enter Value: <input type="text" name="7"><br>
Enter Value: <input type="text" name="8"><br>
Enter Value: <input type="text" name="9"><br>
Enter Value: <input type="text" name="10"><br>
<input type="submit"value="Union of Array" name="Sort">
</form>
<?php
if (isset($_POST['Sort'])){
function arrsort(){
$array=array(10);
$array[0]=$_POST['1'];
$array[1]=$_POST['2'];
$array[2]=$_POST['3'];
$array[3]=$_POST['4'];
$array[4]=$_POST['5'];
$array[5]=$_POST['6'];
$array[6]=$_POST['7'];
$array[7]=$_POST['8'];
$array[8]=$_POST['9'];
$array[9]=$_POST['10'];
for ($i=0;$i<9;$i++)
{
for ($j=0;$j<10-$i-1;$j++){
if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
}
}
echo '<br> The sorted array <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]." ";
}
arrsort();
}
?>
</body>
</html>

Words sorting in lexicographical order

Here, we are showing you the code of words in lexicographical order.

<?php
$array=array('a','b','d','e','k','q','f','z','m','c');
echo 'Words <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]. " ";
for ($i=0;$i<9;$i++)
{
for ($j=0;$j<10-$i-1;$j++)
if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
}
echo '<br> Entered words in lexicographical order <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]." ";
?>

Output

Words
a b d e k q f z m c
Entered words in lexicographical order
a b c d e f k m q z

Words sorting in lexicographical order using the form

Here, we are showing you the code of words in lexicographical order using form.

<?php
$array=array('a','b','d','e','k','q','f','z','m','c');
echo 'Words <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]. " ";
for ($i=0;$i<9;$i++)
{
for ($j=0;$j<10-$i-1;$j++)
if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
}
echo '<br> Entered words in lexicographical order <br>';
for ($i=0;$i<10;$i++)
echo $array[$i]." ";

?>

Output

Figure: sort words in lexicographical order PHP

Words sorting in lexicographical order using the while loop

Here, we are showing you the code of words in lexicographical order using while loop.

<?php
$array=array('a','b','d','e','k','q','f','z','m','c');
echo 'Words <br>';
$i=0;

while($i<10){

echo $array[$i]. " ";

$i++;}

$i=0;
while ($i<9){


$j=0;
while ($j<10-$i-1){

if ($array[$j]>$array[$j+1])
{
$t = $array[$j+1];
$array[$j+1]=$array[$j];
$array[$j]=$t;
}
$j++;
}

$i++;}

echo '<br> Entered words in lexicographical order <br>';
$i=0;
while ($i<10){

echo $array[$i]." ";
$i++;
}
?>

Output

Words
a b d e k q f z m c
Entered words in lexicographical order
a b c d e f k m q z

Topic Covered

How to Sort an array in PHP.

Exit mobile version