0

这些是后变量。

我怎样才能把这些放在循环中?

其中 'master_customer_id' 和 'firstname' 是名称属性,其中的数组是检索到的值,我需要将这些值保存到数据库中。

值是动态的,意思是说数组可以达到任何数字,这里我只是说 3 个键值对。

先感谢您。

  array
  'master_customer_id' => 
    array
      0 => string '1' (length=1)
      1 => string '1' (length=1)
      2 => string '1' (length=1)
  'firstname' => 
    array
      0 => string 'a' (length=1)
      1 => string 'a' (length=1)
      2 => string '' (length=0)

我也在添加表格

<?php 

for($i=1;$i<=3;$i++){            
?>

    <input type="hidden" name="master_customer_id[]" value="1" />
First Name: <input type="text" value="" name="firstname[]"/><br/>

<?php  }  ?>
4

3 回答 3

1
foreach($array as $key => $value)  
{  
    foreach ($value as $key1 => $value1)
    {   
        echo $value1;  
    }  
}
于 2013-04-08T07:13:14.800 回答
1

如果保证你有相同数量的master_customer_idand firstname,你可以像这样循环它:

if (isset($_POST['master_customer_id'])) {
  for ($i = 0; $i < count($_POST['master_customer_id']; $i++) {
    $customer_id = $_POST['master_customer_id'][$i];
    $firstname = $_POST['firstname'][$i];
  }
}
于 2013-04-08T07:13:35.143 回答
1
for ($i = 0; $i < count($_POST['master_customer_id']); $i++) {
  echo $_POST['master_customer_id'][$i];
  echo $_POST['firstname'][$i];
}

或者

foreach ($_POST['master_customer_id'] as $key => $value) {
  echo $value;
  echo $_POST['firstname'][$key];
}
于 2013-04-08T07:14:54.130 回答