Bummer! You must be logged in to access this page.

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

PHP, array_reverse the first element is showing the word "array"

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.

<html> <body> <?php

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

?> <ul> <?php

$list_html = array_reverse($flavors);

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

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

The output is correct, however my first element always shows the word "array" how do i remove it? I tried to use array_shift() however it didn't work.

I solved the question by changing the $list_html to $test, it works the word "array" doesn't printout anymore.. but i don't understand why haha.

7 Answers

Hi Suatyee! I know you've solved your own problem, but just so that you understand what was originally going wrong I'll explain anyway :) - I always hate moving on when I'm still not sure of something!

Ok, so your ForEach iterates over your array, writing the code within the ForEach block for each item in the array ("Jalapeno So Spicy", "Avocado Chocolate", etc. ).

So each iteration ADDS <li> $flavor </li> to the end of $list_html.

The reason you had "array" appear at the beginning, before the first <li> $flavor </li>
is that, before the ForEach started, you had set the value of $list_html to the array.

So before the ForEach had even started, $list_html already had the value of the array, which was then added too with the ForEach.

When you've changed the variable name, depending on which ones you've changed, you are likely not setting the value of the variable that is added to, to array first.

Hope this makes sense!

Also, I don't know if the tutorial your working on says to do this, but you'll see later, in the Shirts for Mike project that when writing html with a ForEach you'll start with something like this :

$list_html = "";
ForEach.....
    $list_html = $list_html . "<li>";
    ........

notice that $list_html is set to "" which of cause is an empty string

Oh! so you have to ensure that the $list_html is empty first, or else it will have the value of "array" right? thanks!

you got it :)

That is the best answer:

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

?> <ul> <?php $flavors_reverse = array_reverse($flavors);

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

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

However I wish sometimes they explain the problem better

Hi

i am having problems with this too. my code is

<html> <body> <?php

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

?> <ul> <?php $list_html = array_reverse($flavors); 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>

it will display the list but the code test fails me. Could anyone help please

$reversed_flavors = array_reverse($flavor); then loop the new variable that will do.

That is the best answer:

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

?> <ul> <?php $flavors_reverse = array_reverse($flavors);

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

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

However I wish sometimes they explain the problem better

Hey there,

I got this problem fixed by putting the array_reverse command in the foreach area. A little weird, but it is () inside of a (). I'm trying to be vague on purpose so I don't give away the answer right away.

Hey there,

I got this problem fixed by putting the array_reverse command in the foreach area. A little weird, but it is () inside of a (). I'm trying to be vague on purpose so I don't give away the answer right away.