如何循环这个数组

时间:2013-04-08 07:10:05

标签: php

这些是职位变量。

如何将它们放入循环中?

其中'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  }  ?>

3 个答案:

答案 0 :(得分:1)

foreach($array as $key => $value)  
{  
    foreach ($value as $key1 => $value1)
    {   
        echo $value1;  
    }  
}

答案 1 :(得分:1)

如果保证您拥有相同数量的master_customer_idfirstname,则可以像这样循环:

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];
  }
}

答案 2 :(得分: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];
}