我想在我的下面的代码中只使用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."')";
}
答案 0 :(得分: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
}
答案 1 :(得分:1)
您可以使用array_map
foreach ( array_map(null, $store_control_text, $store_data) as $join ) {
list($text, $data) = $join;
//Do your stuff
}
答案 2 :(得分: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);