0

Hi I have an array $months which holds the months of the year. I have a funcion echoArray($theArray) which contains a for loop to print out the months as month 1 is January, month 2 is February etc. I now have to create another function reverseArray which will output the months as month 1 is December, month 2 is November etc. However it states not to use the array_reverse() function or any of its aliases. Just wondering if anyone could tell me how to do this, the only way i knew how to reverse an array was the reverseArray() function.

This is the code i have for the echoArray($theArray) function.

function echoArray($theArray)
         {

         $size=count($theArray);

         for ($i=0; $i<=$size-1; $i++)
        echo "month number " . ($i+1) . " is :" .$theArray[$i] . "<br />";
         }
4
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by blocblue
0

It was a piece of work i had at university, I was redoing all the work as I'm starting placement soon and want to get as much experience as possible. I think it was stating not to use the array_reverse so that we knew other ways to complete the work. I tried using a for loop and decrementing however it just decremented the month number 12 is decemeber then month 11 is novemeber. What i need it to do is display month one as Decemeber etc

0

You need to reverse the array, and then print it.

function reverse_array(array $array)
{
    $temp = array();
    $count = count($array);

    for($i = $count - 1; $i = 0; $i--)
        $temp[] = $array[$i];

    return $temp;
}

$array = reverse_array($array);
echoArray($array);

Edited to include zero index as per @iamthwee comment

Edited by blocblue

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.