内爆数组PHP

时间:2017-01-12 05:09:21

标签: php arrays implode

我破坏了这样的代码:

$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";

我该怎么做?

谢谢。

2 个答案:

答案 0 :(得分: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 = '字符串中添加引号来实现此目的,但是Suchit会打败我。

答案 1 :(得分: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