douyuan5600 2011-12-20 09:13
浏览 21
已采纳

内爆阵列发出错误

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)
  • 写回答

6条回答 默认 最新

  • driuwt9557 2011-12-20 09:26
    关注

    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 | 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!