内爆阵列发出错误

时间:2011-12-20 09:13:16

标签: php arrays insert implode

我正在尝试内爆一个数组来执行插入,但我无法触发我所做的错误。 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)

6 个答案:

答案 0 :(得分:1)

尝试:

implode(',', $productcount['attcode']);
//same for others

答案 1 :(得分:1)

您的数组结构不符合所需的输出格式。所以内爆不会奏效。

<?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 | 

答案 2 :(得分:0)

implode使用数组而不是它的值,它会附加带有数组元素的所需字符串。您正在将数组值传递给implode。请检查this以获取详细信息

答案 3 :(得分:0)

您遗漏了错误消息中最重要的部分;其中说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 );

答案 4 :(得分:0)

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;

答案 5 :(得分:0)

我认为您为所需的输出构建了错误的数组:

$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;