我如何在其他区域内使用带有变量名称的php变量,就像我自己键入变量名一样?

时间:2018-05-11 19:37:26

标签: php arrays

以下是我需要帮助的完整代码。该代码有一些注释可以帮助您排除故障并解释我需要的内容,但我也会在上面的代码中进行解释。

问题出在while循环中,或者问题可能在while循环之外,我不知道,但是当我回显$codemsg时,它不会产生正确的事情。我让$codemsg等于一个带有变量的句子。我需要以不同的方式创建该句子,基于从其他地方引入的变量,如数据库。该变量是名为$db_message的顶级变量。

我接受该变量,替换部分字符串,然后我想在while循环中使用它,这就是我无法弄清楚如何做,或者如何格式化它。您可以在我创建$outgoing变量的部分,我尝试的不同内容以及while循环中看到,我试图引入我在上面创建的最终$db_message变量的不同方式

$db_message = "{first} {last}, We will give you up to {value} for your {year} {make} {model}. Visit {url} or call {phone}";
echo "<p>db_message before:<br>".$db_message."</p>";

//convert tags to php variables
$incoming = ["{first}", "{last}", "{value}", "{year}", "{make}", "{model}", "{url}", "{phone}"];

$d = "data";
$dbr0 = "[0]"; $dbr1 = "[1]"; $dbr2 = "[2]"; $dbr3 = "[3]"; $dbr4 = "[4]"; $dbr5 = "[5]"; $dbr6 = "[6]"; $dbr7 = "[7]";

//i don't know if the problem is here, in creating the $outgoing variable
//$outgoing = ["$data[0]", "$data[1]", "$data[2]", "$data[3]", "$data[4]", "$data[5]", "$data[6]", "$data[7]"];
//$outgoing = ["\$data[0]", "\$data[1]", "\$data[2]", "\$data[3]", "\$data[4]", "\$data[5]", "\$data[6]", "\$data[7]"];
//$outgoing = ["\".\$data[0].\"", "\".\$data[1].\"", "\".\$data[2].\"", "\".\$data[3].\"", "\".\$data[4].\"", "\".\$data[5].\"", "\".\$data[6].\"", "\".\$data[7].\""];
//$outgoing = ["$$d[0]", "$$d[1]", "$$d[2]", "$$d[3]", "$$d[4]", "$$d[5]", "$$d[6]", "$$d[7]"];
$outgoing = ["$$d$dbr0", "$$d$dbr1", "$$d$dbr2", "$$d$dbr3", "$$d$dbr4", "$$d$dbr5", "$$d$dbr6", "$$d$dbr7"];

$db_message = str_replace($incoming, $outgoing, $db_message);
echo "<p>db_message after:<br>".$db_message."</p>";

//csv header line
//first | last | value | year | make | model | url | phone | code

//example records for csv file
//test | me   | 3001 | 2010 | toyota  | camry   | http://testme.some-site.com   | 1-888-888-8888 | 111111
//test | you  | 3002 | 2010 | nissan  | maxima  | http://testyou.some-site.com  | 1-888-888-8888 | 222222
//test | him  | 3003 | 2010 | honda   | civic   | http://testhim.some-site.com  | 1-888-888-8888 | 333333
//test | her  | 3004 | 2010 | hyundai | sonata  | http://testher.some-site.com  | 1-888-888-8887 | 444444
//test | them | 3005 | 2010 | subaru  | legacy  | http://testthem.some-site.com | 1-888-888-8887 | 555555
//test | us   | 3006 | 2010 | acura   | integra | http://testus.some-site.com   | 1-888-888-8887 | 666666

//array built from csv file
$handle = fopen("test.csv", "r");
$row = 0;
$responseMessages = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($data[8] !== "code") { //leave out header line
        $code = $data[8];

        //this works, but i'm trying to build this from variable up top
        //$codemsg = "$data[0] $data[1], We will give you up to $data[2] for your $data[3] $data[4] $data[5]. Visit $data[6] or call $data[7]"; //this works
        //$codemsg = $data[0]." ".$data[1].", We will give you up to ".$data[2]." for your ".$data[3]." ".$data[4]." ".$data[5].". Visit ".$data[6]." or call ".$data[7]; //this also works

        //trying to make any of the below act like the one above, but none are working

        //i don't know if the problem is here, in bringing in the final $db_message variable, after converting tags to variables
        $codemsg = "$db_message";
        //$codemsg = "".$db_message."";
        //$codemsg = $db_message;
        //$codemsg = "{$db_message}";

        echo $codemsg."<br>";

        //this echo right above should be producing lines like this, which is data coming from csv file:
        //test me, We will give you up to 3001 for your 2010 toyota camry. Visit http://testme.some-site.com or call 1-888-888-8888

        //but instead it's producing lines like this:
        //$data[0] $data[1], We will give you up to $data[2] for your $data[3] $data[4] $data[5]. Visit $data[6] or call $data[7]

        $responseMessages[$code]['body'] = $codemsg;
        $row++;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要使用变量变量。

看看这里:http://www.dummies.com/programming/php/how-to-use-php-variable-variables/

$Reno = 360000;
$Pasadena = 138000;
$cityname = "Reno";
echo "The size of $cityname is ${$cityname}";
$cityname = "Pasadena";
echo "The size of $cityname is ${$cityname}";

输出将是:

The size of Reno is 360000
The size of Pasadena is 138000