PHP Exercise: Sorting an array with sort function

Store these colors in array: ‘red’, ‘green’, ‘blue’, ‘yellow’, ‘purple’
as $colors then update your list of colors to be sorted in alphabetical order.

<?php

$colors = array(
    "red",
    "green",
    "blue",
    "yellow",
    "purple",
);
echo "<h4>before sort </h4>";

$arraylength = count($colors);
for($x = 0; $x < $arraylength; $x++) {
    echo $colors[$x] . "<br>";
}

sort($colors);

echo "<h4>After sort </h4>";

$arraySortLength = count($colors);
for($x = 0; $x < $arraySortLength; $x++) {
    echo $colors[$x] . "<br>";
}

Recent graduated from Universitas Gadjah Mada, Yogyakarta, Indonesia and work as a Researcher at National Institute of Posts, Telecoms and ICT. Interested in Pattern Recognition and Artificial Intelligent.

Leave a reply:

Your email address will not be published.

Site Footer