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)


         for ($i=0; $i<=$size-1; $i++)
        echo "month number " . ($i+1) . " is :" .$theArray[$i] . "<br />";

Recommended Answers

All 6 Replies

Iterate through the array using a for loop, but start at the highest index and decrement.

why is it stating not to use array_reverse()?

What is this for?

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

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);

Edited to include zero index as per @iamthwee comment

Member Avatar

^That would miss the array indexed at zero.

Good spot. Code updated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.