2014-05-24 263 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

回答明顯重複的問題會鼓勵他們繼續發帖。 –