场景:
我的 PHP 数组定义了键名,情况如下:
array(
'filed1'=>array(1,2,3),
'filed2'=>array(a,b,c),
'filed3'=>array(x,y,z),
);
预期:
我根据这个数组要生成 sql :
INSERT INTO `contents` (`filed1`,`filed2`,`filed3`) VALUES ((1,a,x) ,(2,b,y) ,(3,c,z) ) ;
问题:
$value_str='(';
for($i=0;$i<count( $data[ 0 ] );$i++) // 记录数
{
for($j=0;$j<count($data);$j++) // 字段数量
{
$value_str.="'".$data[$i][$j]."',";
}
}
$value_str.=')';
$filed_str='(`'.implode("`,`",array_keys($data)).'`)';
就在记录数哪里,使用了索引值,$data[0]这样的方式,结果,就出问题了!数组越界 Undefined offset: 0
SO:
定义了键名的数组,就不能用索引来取值了吗?