我试图内爆一个数组来执行插入,但我无法触发我所做的错误。 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)
您的数组结构不符合所需的输出格式。所以内爆是行不通的。
<?php
$my_array = ARRAY();
$my_array['attcode'] = Array ( 0 => 0, 1 => 1, 2 => 2);
$my_array['color'] = Array ( 0 => 'red', 1 => 'green', 2 => 'blue');
$my_array['size'] = Array ( 0 => 100, 1 => 200, 2 => 300);
$my_array['stock'] = Array ( 0 => 11, 1 => 22, 2 => 33);
$loop_me = count($my_array['attcode']) - 1;
for ($i=0; $i<=$loop_me; $i++) {
echo '<div>Code: '.$my_array['attcode'][$i].' | Color: '.$my_array['color'][$i].' | Size: '.$my_array['size'][$i].' | Stock: '.$my_array['stock'][$i].'</div>';
}
?>
输出
Code: 0 | Color: red | Size: 100 | Stock: 11 |
Code: 1 | Color: green | Size: 200 | Stock: 22 |
Code: 2 | Color: blue | Size: 300 | Stock: 33 |
尝试:
implode(',', $productcount['attcode']); //其他人也一样
implode 适用于数组而不是值,它将所需的字符串附加到数组元素。您正在将数组值传递给内爆。请检查this了解详情
您遗漏了错误消息中最重要的部分;它说 implode 需要一个数组并且您已经传递了一个字符串。根据所需的输出,我认为您想要这样的东西:
<?php
$productcount = array(
'attcode' => array ( '0', '1', '2' ),
'color' => array ( 'red', 'green', 'blue' ),
'size' => array( '0', '1', '2' ),
'stock' => array ( 100, 200, 300 )
);
$outcome = array( );
foreach( $productcount['attcode'] as $index => $code ) {
$outcome[] = array(
'attcode' => $code,
'color' => isset( $productcount['color'][$index] ) ? $productcount['color'][$index] : null,
'size' => isset( $productcount['size'][$index] ) ? $productcount['size'][$index] : null,
'stock' => isset( $productcount['stock'][$index] ) ? $productcount['stock'][$index] : null
);
}
var_dump( $outcome );
http://php.net/manual/en/function.implode.php
$attstring = array();//array for storing query set
foreach($productcount as $attributeCount){
$attstring[] = "'" . implode("','", $attributeCount)."'";
}
$finalvalue = "(" . implode("), (", $attstring) . ")";
echo $finalvalue;
我认为你为你想要的输出构建了错误的数组:
$products = array();
$products[] = array('attrcode' => 'XXXX', 'color' => 'black', 'size' => '12', 'stock' => 'yes');
$products[] = array('attrcode' => 'XXXX', 'color' => 'white', 'size' => '5', 'stock' => 'no');
$imploded_products = array();
foreach ($products as $product) {
$imploded_products[] = "'".implode("','", $product)."'";
}
$finalvalue = "(".implode("), (", $imploded_products).")";
echo $finalvalue;