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条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算