Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Refactoring the Codebase Manipulating an Array

Andrew Norris
Andrew Norris
4,857 Points

Code challenge - reversing the order of an array.

I thought that the array function, array_reverse was the key to this challenge

index.php
<html>
<body>
<?php

$flavors = array(
        "Jalapeno So Spicy",
        "Avocado Chocolate",
        "Peppermint",
        "Vanilla",
        "Cake Batter",
        "Cookie Dough"
    );
array_reverse($flavors);
?>
<ul>
<?php

    $list_html = "";
    foreach($flavors as $flavor) {
        $list_html = $list_html . "<li>";
        $list_html = $list_html . $flavor;
        $list_html = $list_html . "</li>";
    }
    echo $list_html;

?>
</ul>
</body>
</html>

1 Answer

Tom Sager
Tom Sager
18,987 Points

It is, but it returns a new array with the items reversed. The original array stays in the same order. Try:

$flavors_reversed = array_reverse($flavors);
Andrew Norris
Andrew Norris
4,857 Points

Thank you. Your advice was very helpful. It took me a few minutes, but I finally figured out that in addition to setting up the reversed array in a new variable, $flavors_reversed, I also had to sub it for $flavors to display the reversed array results.