drmy1050 2011-12-14 08:37
浏览 28
已采纳

多维数组循环PHP

For some reason I cannot set the loop time because I am generating the form dynamically using jQuery. I have done some research for the topic,normally will use foreach to loop for all valid fields but I'm not sure how to do these:

<form action="testing.php" method="post" >
<input type="text" name="product[1][name]" value="product1"/>
<input type="text" name="product[1][color][]"  value="product1color1"/>
<input type="text" name="product[1][color][]"  value="product1color2"/>
<input type="text" name="product[1][color][]"  value="product1color3"/>

<input type="text" name="product[2][name]" value="product2"/>
<input type="text" name="product[2][color][]"  value="product2color1"/>

<input type="text" name="product[3][name]" value="product3"/>
<input type="text" name="product[3][color][]"  value="product3color1"/>


<input type="text" name="product[4][name]" value="product4"/>
<input type="text" name="product[4][color][]"  value="product4color1"/>

<input type="submit" />

And my testing code ended up like these, it is not working .=(

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

$product=$_POST['product'];
//store everything that start with product into array


   foreach($product as $key){
   //loop for product.1 product.2 and so on.....

      //echo name of current product
      echo $product[$key]['name'];

          foreach($product[$key]['color'][] as $point){
          echo $point;
          }//loop for every single available color field

   }//end of product loop
}// end of post request

?>
  • 写回答

2条回答 默认 最新

  • doujiayao8433 2011-12-14 08:51
    关注

    The statement $product = $_POST['product']; will arrange the data from your HTML form into a multidimensional array that looks like this:

    Array(
        1 => Array(
            "name" => "product1",
            "color" => Array(
                0 => "product1color1",
                1 => "product1color2",
                2 => "product1color3"
            )
        ),
        2 => Array(
            "name" => "product2",
            "color" => Array(
                0 => "product2color1"
            )
        ),
        ...
    )
    

    To iterate this array, you need to do:

    foreach($product as $key1 => $prd) {
        echo $prd['name'];
        foreach($prd['color'] as $key2 => $point) {
            echo $point;
        }
    }
    

    On each pass, $key1 will contain the array keys (1, 2, 3) whereas $prd will contain the item associated with that key ($product[1], $product[2], $product[3]). You can omit the $key => portion altogether if necessary. Likewise for the inner loop.

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

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下