2017-01-12 62 views
0

我有爆這樣的代碼:破滅陣列PHP

$date_condition = implode(" AND a.date = ",$date_interval); 

該數組是這樣的:

$date_interval = Array('2017-01-24','2017-01-25','2017-01-26'); 

這是我想要有望成爲輸出什麼:

$date_condition = "'2017-01-24' AND a.date = '2017-01-25' AND a.date = '2017-01-26' "; 

但是從我的代碼我嘗試我只是得到這樣的輸出:

$date_condition = "2017-01-24 AND a.date = 2017-01-25 AND a.date = 2017-01-26"; 

我該怎麼做?

謝謝。

+1

的複製[此](http://stackoverflow.com/questions/10490860/php-implode-but-wrap-each-element-in-引用)所以問題? –

+1

[PHP內部爆發但是在引號中包裹每個元素]的可能重複(http://stackoverflow.com/questions/10490860/php-implode-but-wrap-each-element-in-quotes) – Xorifelse

回答

0

嘗試這種情況:

$date_interval = Array('2017-01-24','2017-01-25','2017-01-26'); 
$date_condition ="'". implode("' AND a.date = '",$date_interval)."'"; 
echo $date_condition; 

DEMO HERE

0

要麼改變數組:

$date_interval = Array("'2017-01-24'","'2017-01-25'","'2017-01-26'"); 

遍歷它:

foreach($date_interval as $k => $v){ 
    $date_interval[$k] = "'$v'"; 
} 

創建一個函數:

function quotate(array $list){ 
    foreach($list as $v){ 
    $ret[] = "'$v'"; 
    } 

    return $ret; 
} 

$date_condition = implode(" AND a.date = ",quotate($date_interval)); 

或通過添加引號來此' AND a.date = '串做到這一點,但蘇奇特打敗了我。