解析由html表单发布的php中的数组

时间:2014-03-01 16:05:18

标签: php html mysql arrays forms

我有以下问题。 我在表单中有这些类型的选择:

<select name="variation[8][]" multiple>
   <option value="1">s</option>
   <option value="2">m</option>
   <option value="3">l</option>
</select>

<select name="variation[9][]" multiple>
   <option value="8">red</option>
   <option value="9">blue</option>
   <option value="10">black</option>
</select> 

我想用php正确阅读发布的数组,我想在数据库中写这个:

variation_id => 8 
variation_term => 1 
variation_id => 8 
variation_term => 2 
variation_id => 8 
variation_term => 3 

variation_id => 9 
variation_term => 8 
variation_id => 9 
variation_term => 9 
variation_id => 9 
variation_term => 10

如何解析发布的这个数组? 还有更好的方法吗?

TNX

我找到了这个解决方案:

foreach ($_POST['variation'] as $key => $value) {
   for($i=0; $i<count($value); $i++){
     echo "varitaion name = $key"."<br>";
     echo "value = $value[$i]";
   }
}

1 个答案:

答案 0 :(得分:1)

尝试

foreach($_POST['variation'] as $key=>$val){
  if(is_array($val)){
     echo " Values for $key Are:"; // get your variation index eg:8,9 etc
     foreach($val as $key1=>$val1){
         echo $val1.","; // get the index values
     }
         echo "<br/>";
  }
}