数组以逗号分隔?

时间:2014-05-24 15:50:21

标签: php

 $sql = "SELECT product_idapi FROM `product` where seller_id = 1300";
 $data = $connection->fetchAll($sql);
 //$data = implode(', ', $data);
 print_r($data);
 exit;

我的数组看起来像:

Array (
[0] => Array ( [product_idapi] => 416252 )
[1] => Array ( [product_idapi] => 70328968 )
[2] => Array ( [product_idapi] => 416240 )
[3] => Array ( [product_idapi] => 70324800 )
[4] => Array ( [product_idapi] => 416255 )
[5] => Array ( [product_idapi] => 70328969 )
[6] => Array ( [product_idapi] => 2270931 )
[7] => Array ( [product_idapi] => 105737962 )
[8] => Array ( [product_idapi] => 73443824 )
[9] => Array ( [product_idapi] => 70318714 )
)

我想:

416252,703289628,416240,70324800 etc...

你可以看到我已经累了内爆会返回“数组,数组,数组”不是真正的值+空格

1 个答案:

答案 0 :(得分:0)

如果你正在使用PHP< 5你会迭代然后内爆而不是

$list = array();

foreach ($data as $d){
    $list[] = $d['product_idapi'];
}

$string = implode(',', $list);

或者对于PHP 5+,正如马里奥建议的那样,使用array_column()implode()

$string = implode(',', array_column($data, 'product_idapi'));