Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.