doufei8691 2015-11-26 19:22 采纳率: 100%
浏览 60
已采纳

PHP Arrays输出理解

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条回答 默认 最新

  • doulanyan6455 2015-11-26 19:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况