Reputation: 1303
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)
Upvotes: 0
Views: 268
Reputation: 2947
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 |
Upvotes: 1
Reputation: 713
I think you've build the wrong array for the output you want:
$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;
Upvotes: 0
Reputation: 6718
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;
Upvotes: 0
Reputation: 18859
You left out the most important part of the error message; where it says that implode expects an array and that you've passed a string. Based on the desired output, I reckon you want something like this:
<?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 );
Upvotes: 0
Reputation: 314
implode works with arrays not it's values, it appends the desired string with array elements. You are passing array value to implode. pls check this for detail
Upvotes: 0
Reputation: 100175
Try:
implode(',', $productcount['attcode']); //same for others
Upvotes: 1