简体   繁体   中英

Implode array PHP

I have implode code like this :

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

That array looks like this :

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

This is what I want to expected become an output :

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

But from my code that I was try I just get output like this :

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

How can I do that ?

Thank you.

Try this:

$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

Either change the array:

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

Loop through it:

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

Create a function:

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

  return $ret;
}

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

Or do it by adding quotes to this ' AND a.date = ' string, but Suchit beat me to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM