将数据转到数组中,并用逗号分隔id


Turn data in array and implode ids with commas

我得到了一个元素,其中显示了一些折扣优惠。每个页面都有相关的报价,所以在我的查询中,我将每个与当前页面相关的报价放在第一位,其余的在后面添加。

因为有多个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的结果,我怎么能得到107108109 ?我试着:

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

输出不是一个数组,所以它不会像那样工作。

我对你的代码做了一些改变:-

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);
}

Implode将用逗号符号

连接所有数组元素。