Maison >développement back-end >tutoriel php >多行数据组合数组问题~~,真心不会组合了,求教导!
类似上面的表单,还没加js,不一定多少行,我要做的是每行一条数据写入数据库。这个数组我就不会组合了
我表单里是如下设置的:
<table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> 品名:<input name="pinming[]" type="text" value=""> 型号:<input name="xinghao[]" type="text" value=""> 规格:<input name="guige[]" type="text" value=""> 数量:<input name="shuliang[]" type="text" value=""> 单价:<input name="danjia[]" type="text" value=""> 总价:<input name="zongjia[]" type="text" value=""> </td> </tr> <tr> <td> 品名:<input name="pinming[]" type="text" value=""> 型号:<input name="xinghao[]" type="text" value=""> 规格:<input name="guige[]" type="text" value=""> 数量:<input name="shuliang[]" type="text" value=""> 单价:<input name="danjia[]" type="text" value=""> 总价:<input name="zongjia[]" type="text" value=""> </td> </tr> </table>
array 'pinming' => array 0 => string '123' (length=3) 1 => string '123' (length=3) 'xinghao' => array 0 => string '123' (length=3) 1 => string '123' (length=3) 'guige' => array 0 => string '123' (length=3) 1 => string '123' (length=3) 'shuliang' => array 0 => string '123' (length=3) 1 => string '123' (length=3) 'danjia' => array 0 => string '123' (length=3) 1 => string '123' (length=3) 'zongjia' => array 0 => string '12312' (length=5) 1 => string '123' (length=3)
array( //第一条 array( 'pinming' => 'xxx' 'guige' => 'xxx' //....其他字段 ) //第二条 array( 'pingming' => 'xxx' 'guige' => 'xxx' //其他字段 ) //第N条 array())
不就一个行列交换么?
class squareArray{ public function swapRowCol($inArr) { $mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); foreach($inArr as $key => $value) { $temArr = new ArrayIterator($value); $mit->attachIterator($temArr, $key); } $arr = array(); foreach($mit as $item) array_push($arr,$item); if (isset($inArr[0])) $arr = array_combine(array_keys($inArr[0]),$arr); return $arr; } public function swapRowColWithKey($inArr) { foreach($inArr as $k1=>$v1) foreach($v1 as $k2=>$v2) $arr[$k2][$k1] = $v2; return $arr; }...
本来你的输入网页就应该设计成一行(一个td)用一组变量名,那不就简单了
foreach($_POST as $name=>$item) { if(is_array($item)) { foreach($item as $key=>$value) { $res[$kek][$name] = $value; } }}$res 就是你要的结果
$fields=array('pinming','guige','xinghao','shuliang','danjia','zongjia');$data=array();foreach($_POST['pinming'] as $key=>$vo){ foreach($fields as $item){ $data[$key][$item]=$_POST[$key][$item]; }}//$data is the result
foreach($_POST as $name=>$item) { if(is_array($item)) { foreach($item as $key=>$value) { $res[$kek][$name] = $value; } }}$res 就是你要的结果
先完成行列转换,然后删去全空的项目
foreach($res as $i=>$item) { if(trim(join('', $item)) == '') unset($res[$i]);}
这种提交没有必要搞这么复杂
只要你确定1点就可以简单化,就是哪一项是必填项,也就是说不能为空的
这里我假设品名不能为空,那么程序之需要这样就可以
foreach($pinming as $key=>$val) { $uparr=array(); if(trim($val)!='') { $uparr['pinming']=$val; $uparr['xinghao']=$pinming[$key]; $uparr['guige']=$guige[$key]; $uparr['shuliang']=$shuliang[$key]; $uparr['danjia']=$danjia[$key]; $uparr['zongjia']=$zongjia[$key]; //这里加插入数据库语句 }}
foreach($_POST as $name=>$item) { if(is_array($item)) { foreach($item as $key=>$value) { $res[$kek][$name] = $value; } }}$res 就是你要的结果
foreach($_POST as $name=>$item) { if(is_array($item)) { foreach($item as $key=>$value) { $res[$kek][$name] = $value; } }}$res 就是你要的结果
foreach($_POST as $name=>$item) { if(is_array($item)) { foreach($item as $key=>$value) { $res[$kek][$name] = $value; } }}$res 就是你要的结果
也就是
$newarr=array();foreach($pinming as $key=>$val) { $uparr=array(); if(trim($val)!='') { $uparr['pinming']=$val; $uparr['xinghao']=$pinming[$key]; $uparr['guige']=$guige[$key]; $uparr['shuliang']=$shuliang[$key]; $uparr['danjia']=$danjia[$key]; $uparr['zongjia']=$zongjia[$key]; $newarr[]=$uparr; }}