2016-11-17 45 views
0

我收到了一個顯示一些折扣優惠的元素。每個頁面都有相關的優惠,所以在我的查詢中,我使得與當前頁面相關的每個優惠都是首先出現,其餘的優惠都是在之後添加的。用逗號打開數組和implode標識

因爲有多個ID我需要將它們從分隔符中分離出來以便在查詢中使用它們(使用ID IN('1', '2'))。我怎樣才能做到這一點?當我有以下查詢:

foreach($offercr as $aanb){ 
    //Haal alles op van fe_elements waar het id gelijk is aan element_id van fe_connections 
    $offer1      = "SELECT * 
            FROM `web_fieldsandfilters_elements` 
            WHERE `id` = '".$aanb['element_id']."'"; 
    $offercon1     = $conn->query($offer1); 
    $offercr1      = array(); 
    while ($offercr1[]   = $offercon1->fetch_array()); 
    foreach($offercr1 as $offerresult){ 
     $offerresultfinal .= $offerresult['item_id']; 
    } 
} 

$offerresultfinal = 107108109結果,我怎麼能得到這個107,108,109?我試過了:

$usable = implode(",", $offerresultfinal); 

輸出不是數組,所以它不能這樣工作。

+2

'$ ITEM_IDS = []; foreach($ offercr1 as $ row){$ item_ids [] = $ row ['item_id']; } $ useable = implode(',',$ item_ids);'? – Cyclonecode

+1

你說得對。我錯過了fetch_array()部分。刪除我的帖子,因爲它是不正確的。謝謝:) – Samir

+0

@Cyclone幾乎在那裏,但這會產生2個逗號而不是1 – twan

回答

0

我已經做了一些改動代碼: -

foreach($offercr as $aanb){ 
    //Haal alles op van fe_elements waar het id gelijk is aan element_id van fe_connections 
    $offer1      = "SELECT * 
            FROM `web_fieldsandfilters_elements` 
            WHERE `id` = '".$aanb['element_id']."'"; 
    $offercon1     = $conn->query($offer1); 
    $offercr1      = array(); 
    while ($offercr1[]   = $offercon1->fetch_array()); 
    $offerresultfinal=[]; 
    $i=0; 
    foreach($offercr1 as $offerresult){ 
     $offerresultfinal[$i]= $offerresult['item_id']; 
     $i=$i+1; 
    } 
    $useable = implode(',', $offerresultfinal); 

} 

破滅將加入所有的逗點符號數組元素