2014-05-24 150 views
0
$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... 

你可以看到我已经厌倦破灭它将返回 “数组,数组,数组” 不实值+空格

+0

你有你的阵列中的阵列,则需要进行迭代。 – Popnoodles

+2

或者使用新的''array_column()'](http://php.net/array_column),然后使用你的'implode()'。 – mario

回答

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')); 
+0

谢谢第一次解决方案第二次不知道为什么什么都不打印 – zumbamusic

+0

您可能有一个小于5的PHP版本,或者在修复我的错误之前尝试过。 – Popnoodles

+0

回答明显重复的问题会鼓励他们继续发帖。 –