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 Object-Oriented PHP Basics Building a Collection Creating a Collection

ISSUE ECHO ARRAY IN PHP

WHY HAD RESULT LIKE THIS I THINK WHEN WANT DISLAY ARRAY SHOULD USE FOREACH WHY IN THIS CASE USE IMPLODE();

Granola                                                                                                                                         
Granola Muffins                                                                                                                                 
Grilled Salmon With Fennel                                                                                                                      
Ground Turkey Pepper Casserole                                                                                                                  
Italian Lemon Chicken                                                                                                                           
Lasagna                                                                                                                                         
Orange Cranberry Scones                                                                                                                         
Pan Seared Pork Tenderloin Rolled In A Chili Peppercrust                                                                                        
Rabbit Catalan                                                                                                                                  
Silver Dollar Pancakes                                                                                                                          
Spicy Omelette 

WHEN USE CODE

<?php
....
echo Render::listRecipes($cookbook->getRecipeTitles());
.....
?>

FUNCTION

 <?php 
   ....
  public static function listRecipes($titles)
      {
          asort($titles);
          return implode("\n",$titles);
      }
.....

 public function getRecipeTitles(){
    $titles = array();
    foreach ($this->recipes as $recipe){//foreach dùng để hiển thị biến mảng 
      $titles[] = $recipe->getTitle();
    }
    return $titles;
  }
?>

1 Answer

Walter Allen
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

Hey there! There are often multiple solutions to a programming problem. If you are wanting to iterate through an array, you can use the foreach method, printing out each element of the array as you go. implode() is a special PHP method that basically does that hard word for you AND has the added benefit of not adding any additional code or output at the end.

For example, if you used the following code:

$titles = "";
    foreach ($this->recipes as $recipe)
      "" .= $recipe->getTitle();
    }
    return $titles;

You will end up with a list of titles; however, they will all be concatenated together with no line breaks like this:

GranolaGranola MuffinsGrilled Salmon With FennelGround Turkey Pepper Casserole  ...                                                                                                                

If you were to add an '/n' to the foreach like this:

$titles = "";
    foreach ($this->recipes as $recipe)
      $titles .= $recipe->getTitle() . '/n';
    }
return $titles;

Then, you would get something better with a newline at the end of each argument, like this:

Granola                                                                                                                                         
Granola Muffins                                                                                                                                 
Grilled Salmon With Fennel                                                                                                                      
Ground Turkey Pepper Casserole                                                                                                                  

But you would have an extra newline character at the end. The implode() function does the same thing for you BUT it does not include the delimiter (in this case, the new line character) at the end of the list. Therefore,

return implode("\n",$titles);

returns the same list as a string with no newline character on the end:

Granola                                                                                                                                         
Granola Muffins                                                                                                                                 
Grilled Salmon With Fennel                                                                                                                      
Ground Turkey Pepper Casserole                                                                                                                  

Does that help? If it still doesn't make sense, let me know, and I'll try to explain a different way. :)