PHP Program to Display Array Elements in Alphabetical Order of an Array

Here is a PHP program to display the array elements in alphabetical order of an array.

<html>
<body>
<?php
$a=array("ashok","babu","amit","shinu","ratan");
$b=count($a);
for($i=0;$i<$b-1;$i=$i+1)
{
for($j=$i+1;$j<$b;$j=$j+1)
{
if(strcmp($a[$i],$a[$j])>0)
{
$temp=$a[$i];
$a[$i]=$a[$j];
$a[$j]=$temp;
}
}
}
echo "The array elements are:-"."<br>";
for($i=0;$i<$b;$i=$i+1)
{
echo $a[$i]."<br>";
}
?>
</body>
</html>

Output: The array elements are:-

amit
ashok
babu
ratan
shinu

See also:

Leave a Comment