php foreach table display on multidimensional Arrays

943
875

Answer

Solution:

Your$DogToys array is a multidimensional array. When you use:

foreach($DogToys as $accessories) {
...
}

each$accessories is also an array -- an array within your array.

Try this instead:

<?php
$DogToys = array();
  $DogToys[0] = array("collar","14.99","red", "smooth");
  $DogToys[1] = array("Bow","4.99","pink", "silky");
  $DogToys[2] = array("booties","24.99","green", "smooth");
  $DogToys[3] = array("Tail Bow","5.99","pink", "Satin");
  $DogToys[4] = array("ear clip","7.99","green", "plastic");

?>

    <h5>Accessories For Sale -- Well Worth a Second Look!</h5>
                    <?php
                    echo "<table>";
                    foreach($DogToys as $accesories) {
                    echo "<tr>";
                    echo "<td>".$accesories[0]."</td>
                          <td>".$accesories[1]."</td>
                          <td>".$accesories[2]."</td>
                          <td>".$accesories[3]."</td>";
                    echo "</tr>";
                    echo "<br >";
                    }       
                    echo "</table>";
                    ?>

Alternately, you could apply what you did to the outer array ($DogToys) to each inner array ($accessories):

    <h5>Accessories For Sale -- Well Worth a Second Look!</h5>
                    <?php
                    echo "<table>";
                    foreach($DogToys as $accesories) {
                      echo "<tr>";
                        foreach ($accessories as $item) {
                            echo "<td>".$item."</td>";
                        }
                      echo "</tr>";
                      echo "<br >";
                    }       
                    echo "</table>";
                    ?>
245

Answer

Solution:

You just need another loop as it's multi-dimensional:

<?php
$DogToys = array();
  $DogToys[0] = array("collar","14.99","red", "smooth");
  $DogToys[1] = array("Bow","4.99","pink", "silky");
  $DogToys[2] = array("booties","24.99","green", "smooth");
  $DogToys[3] = array("Tail Bow","5.99","pink", "Satin");
  $DogToys[4] = array("ear clip","7.99","green", "plastic");

?>

<h5>Accessories For Sale -- Well Worth a Second Look!</h5>
<?php
echo "<table border='1'>"; //to match your imaage
foreach ($DogToys as $each){

    echo "<tr>";
    foreach ($each as $accesories){
        echo "<td>" . $accesories . "</td>";
    }
    echo "</tr>";

}
echo "</table>";
?>

People are also looking for solutions to the problem: php - WordPress Custom Menu Widget ==> change to 2-column

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.