简体   繁体   中英

Multidimensional array looping 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

?>

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.

Your code should be like this

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

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


for($i=0; $i<count($product); $i++){
//   foreach($product as $key){
   //loop for product.1 product.2 and so on.....

      echo "<br/> &nbsp;".$product[$i]['name'];


      for($j=0; $j<count($product[$i]['color']); $j++){
//      foreach($product[$key]['color'][] as $point){
          echo "<br/> &nbsp;&nbsp;".$product[$i]['color'][$j];
          }//loop for every single available color field


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM