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 Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Associative Arrays

issue combined array_walk and use <tag>table in php

I want use it for more convenient. But i don't know why it no work

index.php
<?php $movie = array(
      "title"=>"The Empire Strikes Back",
);
$movie["year"] ="1980";
$movie["director"] ="Irvin Kershner";
$movie["imdb_rating"] ="8.8";
$movie["imdb_ranking"] ="11";
function display($key,$value){

  "<tr>"
    "<th>"echo $key"</th>"
    "<td>"echo $value"</td>"
  "</tr>"
}

?>

<h1><?php echo $movie["title"].$movie["year"] ?></h1>

  <?php array_walk($movie, 'display') ?>
  <tr>
    <th>Director</th>
    <td>Robert Zemeckis</td>
  </tr>
  <tr>
    <th>IMDB Rating</th>
    <td>8.5</td>
  </tr>
  <tr>
    <th>IMDB Ranking</th>
    <td>53</td>
  </tr>
</table>
Thomas Dimnet
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Thomas Dimnet
Python Development Techdegree Graduate 43,629 Points

Hey Tran,

if you do not find a solution yet, try this :

<?php $movie = array(
            "title"=>"The Empire Strikes Back",
      );
      $movie["year"] ="1980";
      $movie["director"] ="Irvin Kershner";
      $movie["imdb_rating"] ="8.8";
      $movie["imdb_ranking"] ="11";
      function display($key,$value){
        return "<tr><th>" . $key . "</th><td>" . $value . "</td></tr>";
      }

      ?>

      <h1><?php echo $movie["title"].$movie["year"] ?></h1>

        <?php array_walk($movie, 'display') ?>
        <tr>
          <th>Director</th>
          <td>Robert Zemeckis</td>
        </tr>
        <tr>
          <th>IMDB Rating</th>
          <td>8.5</td>
        </tr>
        <tr>
          <th>IMDB Ranking</th>
          <td>53</td>
        </tr>
      </table>

You do not have to 'echo' every variables. You still have to work on it but now it displays your results on the screen ;)

1 Answer

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

My guess is that inside your display function, you're not returning or echoing anything. If just strings that are just kinda hanging around.

function display($key,$value){

  "<tr>"
    "<th>"echo $key"</th>"
    "<td>"echo $value"</td>"
  "</tr>"
}

Try echoing the string together

function display($key,$value){
    echo "<tr><th>{$key}</th><td>{$value}</td></tr>";
}

Or if you'd prefer concatenation.... Try echoing the string together

function display($key,$value){
    echo "<tr><th>" . $key . "</th><td>" . $value . "</td></tr>";
}

Thanks