【发布时间】:2012-01-24 06:37:19
【问题描述】:
我正在尝试内爆一个数组以执行插入,但我无法触发我所做的错误。 implode() [function.implode]:传递的参数无效 *请注意我的数组大小不固定,所以我使用了 foreach*
数组结构
[attcode] => Array ( [0] => [1] => [2] => )
[color] => Array ( [0] => [1] => [2] => )
[size] => Array ( [0] => [1] => [2] => )
[stock] => Array ( [0] => [1] => [2] => )
工作代码
$attstring = array();//array for storing query set
foreach($productcount['attcode'] as $attcode){
$attstring[] = "'" . implode("','", $attcode)."'";
}
foreach($productcount['color'] as $attcolor){
$attstring[] = "'" . implode("','", $attcolor)."'";
}
foreach($productcount['size'] as $attsize){
$attstring[] = "'" . implode("','", $attsize)."'";
}
foreach($productcount['stock'] as $attstock){
$attstring[] = "'" . implode("','", $attstock) . "'";
}
$finalvalue = "(" . implode("), (", $attstring) . ")";
echo $finalvalue;
期望的输出
('code','color','size',stock),
('code','color','size',stock),
('code','color','size',stock)
【问题讨论】:
-
我很想看看实际的阵列布局。是 0、1、2 的值吗?钥匙?
-
foreach 块中的内爆是错误的,因为您没有传递数组
-
foreach 正在遍历数组,所以 $attcode 是一个字符串,而不是数组,这就是 implode 不起作用的原因。