-1

我只想在下面的代码中使用 foreach 一次。我需要将两个数组值即 store_data 和 store_control_text 复制到我的数据库各自的字段中。需要建议不要对多个数组值使用 foreach 循环。

$data = $_REQUEST['columns_one'];
$controlText = $_REQUEST['controlText'];

$store_control_text = explode(",",$controlText);
$store_data = explode(",",$data);

$query = "INSERT INTO formdetailstemp(FormId,ControlType,ControlText,ControlPara1,Mandatory) VALUES ";
$values = array(); //store all the new rows
foreach($store_data as $key =>$value){
  $values[] = "('','".$value."','".$controlText."','".$controlPara."','".$mandatoryValue."')";
}
4

3 回答 3

1

我相信您正在寻找一个MultipleIterator,它将通过一个循环轻松地遍历两个数组foreach

$iter = new MultipleIterator;
$iter->attachIterator( new ArrayIterator( $store_control_text));
$iter->attachIterator( new ArrayIterator( $store_data));

foreach( $iter as $data) {
    list( $a, $b) = $data;
    var_dump( $a, $b);
    // Do your SQL insert with $a and $b
}
于 2012-11-03T19:33:14.320 回答
1

您可以使用array_map

foreach ( array_map(null, $store_control_text, $store_data) as $join ) {
    list($text, $data) = $join;
     //Do your stuff 

}

查看简单示例

于 2012-11-03T19:37:36.913 回答
0
you can use .implode();

foreach ($_POST['description'] as $row=>$name){
$description = $name;
$supplier = $_POST['supplier']; //Normal textbox value
$quantity = $_POST['quantity'][$row]; //Array text box 1
$partnumber = $_POST['partnumber'][$row]; // Array text box 2
$unitcost = $_POST['unitcost'][$row]; // Array text box 3

$sql_array[] = '("NULL","' . $partnumber . '","' . $supplier . '","'.$description.'", "'.$quantity.'","'.$unitcost.'")'; 
}

    $query_single = 'INSERT INTO `inwards` (`id`, `partnumber`, `suppliers`, `description`, `quantity`,`unitcost`) VALUES '. implode(', ', $sql_array);
于 2017-09-06T06:49:29.667 回答