I'm trying to implode an array to perform insertion , but i couldn't trigger the error i did. implode() [function.implode]: Invalid arguments passed Please note my array size is not fix so I used foreach
Array structure
[attcode] => Array ( [0] => [1] => [2] => )
[color] => Array ( [0] => [1] => [2] => )
[size] => Array ( [0] => [1] => [2] => )
[stock] => Array ( [0] => [1] => [2] => )
Working code
$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;
Desired output
('code','color','size',stock),
('code','color','size',stock),
('code','color','size',stock)
Your array structure does not fit the desired output format. So implode won't work.
<?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>';
}
?>
Output
Code: 0 | Color: red | Size: 100 | Stock: 11 |
Code: 1 | Color: green | Size: 200 | Stock: 22 |
Code: 2 | Color: blue | Size: 300 | Stock: 33 |