如何将数组中的值显示在php字符串中?

时间:2011-06-03 16:23:12

标签: php cakephp

我在一个数组中有两个值,我希望它们显示在我正在构建的字符串变量中。这些值是动态的,可能会发生变化,因此我希望能够直接从数组中构建字符串。我以为自己走在了正确的轨道上,但价值观没有出现。

我的代码:

$range = "WHERE transdate >= \"{$halfway['startdate']}\" AND transdate <= \"{$halfway['enddate']}\"";

输出:

WHERE transdate >= "" AND transdate <= ""

3 个答案:

答案 0 :(得分:4)

以下适用于php 5.3.5 cli。我没有修改你的字符串

$halfway = array(
    'startdate' => 'somedate',
    'enddate' => 'otherdate'
);

$range = "WHERE transdate >= \"{$halfway['startdate']}\" AND transdate <= \"{$halfway['enddate']}\"";

print $range;

输出:

WHERE transdate >= "somedate" AND transdate <= "otherdate"

检查$halfway是否确实有数据。

答案 1 :(得分:1)

尝试删除数组元素中的引号。我认为这样可行..

WHERE transdate >= \"{$halfway[startdate]}\" AND transdate <= \"{$halfway[enddate]}\"

答案 2 :(得分:0)

为什么使用框架而不使用其ORM?

$this->ModelName->find('all', array(
    'conditions' => array(
        'ModelName.transdate BETWEEN ? AND ?' => array(
            $halfway['startdate'],
            $halfway['enddate']
         )
    )
));