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

my array_reverse doesn't work

This block of code creates an array of ice cream flavors and displays them in ascending order. The owner of the ice cream shop would like this order changed so that the flavors are displayed in descending order instead, starting with Cookie Dough first and Jalapeno So Spicy last. Leave the $flavors array itself in the same order, but modify something else in this code block to achieve that.


i just add the var with array_reverse and changed $flavors in the foreach with my var but doesn't work

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

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

<ul>
<?php

    $list_html = "";
    foreach($flavorsReverese 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

geoffrey
geoffrey
28,724 Points

What you do is a way to achieve the result, but they don't expect you to do that this way. You have to yo use the array_reverse function directly inside the foreach.

If you read carefully the instructions It says:

Leave the $flavors array itself in the same order, but modify something else in this code block to achieve that.

What you need to modify is this snippet of code:

<?php

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

?>