1

Hi I have recently just started PHP and I'm currently trying to learn arrays. Below is an array im looking at and don't quite understand how it outputs the columns of the array. I don't understand how $row and $col suddenly just output the first 2 columns of the array and also what would need to do to output the 3rd column.

$shop = array( array("rose", 1.25 , 15),
               array("daisy", 0.75 , 25),
               array("orchid", 1.15 , 7) 
             ); 

             echo "<h1>Using loops to display array elements</h1>";

echo "<ol>";
for ($row = 0; $row < 3; $row++)
{
    echo "<li><b>The row number $row</b></li>";
    echo "<ul>";

    for ($col = 0; $col < 3; $col++)
    {
    echo $shop [$row] [$col];
    }

    echo "</ul>";
    echo "</li>";
}
echo "</ol>";

3 Answers 3

4

Actually, the three rows ARE displayed when you execute the script. When tested with php 5.5, the output was:

<h1>Using loops to display array elements</h1>
<ol>
  <li><b>The row number 0</b></li>
  <ul>rose1.2515</ul></li>
  <li><b>The row number 1</b></li>
  <ul>daisy0.7525</ul></li>
  <li><b>The row number 2</b></li>
  <ul>orchid1.157</ul></li>
</ol>

The problem here is your HTML syntax. If you look closely, you can see that every <ul> block are followed by a </li>, which is never opened by an expected <li>.

To respect the standard, you should also put every element of each <ul> you generate with a <li> block.

Result should look something like this:

<h1>Using loops to display array elements</h1>
<ol>
  <li><b>The row number 0</b></li>
  <ul>
    <li>rose</li>
    <li>1.25</li>
    <li>15</li>
  </ul>
  <li><b>The row number 1</b></li>
  <ul>
    <li>daisy</li>
    <li>0.75</li>
    <li>25</li>
  </ul>
  <li><b>The row number 2</b></li>
  <ul>
    <li>orchid</li>
    <li>1.15</li>
    <li>7</li>
  </ul>
</ol>

Hope this helps!

Edit: You could also use the PHP native function array_column, here's the documentation link.

Sign up to request clarification or add additional context in comments.

Comments

1

It does output all three columns, but a bit mashed up. Try adding a space or something between the values:

echo $shop[$row][$col] . '&nbsp;';

Or display it in a table:

echo "<table border='1'>";
for ($row = 0; $row < 3; $row++)
{
    echo "<tr><td>Row number $row</td>";

    for ($col = 0; $col < 3; $col++)
    {
        echo '<td>' . $shop[$row][$col] . '</td>';
    }
    echo "</tr>";
}
echo "</table>";

2 Comments

Thanks for your help and your way works great but could ask a beginners question about how . '&nbsp;'; makes spaces for all three inputs even though you've placed it a the end of the echo. Why doesn't it just add one space.
For every row, you run the echo 3 times (for .. $col) and every time you print the value of the column ( 0 to 2 ) followed by a '&nbsp;' (no-breakable space), giving every column value a space after the value.
0
$shop = array( array("rose", 1.25 , 15),
               array("daisy", 0.75 , 25),
               array("orchid", 1.15 , 7) 
             ); 

             echo "<h1>Using loops to display array elements</h1>";

echo "<ol>";
for ($row = 0; $row < count($row); $row++)
{
    echo "<li><b>The row number $row</b></li>";
    echo "<ul>";

    for ($col = 0; $col < count($shop[$row]); $col++)
    {
        echo "<li>";
        echo $shop [$row] [$col];
        echo "</li>";
    }

    echo "</ul>";
    echo "</li>";
}
echo "</ol>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.