如何使用foreach遍历带有变量变量的文件?

How to use foreach to loop through a file with variable variable?

提问人:user6332864 提问时间:6/12/2016 最后编辑:user6332864 更新时间:6/12/2016 访问量:387

问:

以下使用 foreach 的 PHP 代码似乎不起作用。我相信它与“”有关。<a href='/$value/access'>

我已经分享了整个代码库。

有谁知道我的陈述有什么问题?

包含/函数:.php

<?php

    // Defining the basic cURL function
    function curl($url) {
        // Assigning cURL options to an array
        $options = Array(
            CURLOPT_RETURNTRANSFER => TRUE,  // Setting cURL's option to return the webpage data
            CURLOPT_FOLLOWLOCATION => TRUE,  // Setting cURL to follow 'location' HTTP headers
            CURLOPT_AUTOREFERER => TRUE, // Automatically set the referer where following 'location' HTTP headers
            CURLOPT_CONNECTTIMEOUT => 120,   // Setting the amount of time (in seconds) before the request times out
            CURLOPT_TIMEOUT => 120,  // Setting the maximum amount of time for cURL to execute queries
            CURLOPT_MAXREDIRS => 10, // Setting the maximum number of redirections to follow
            CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  // Setting the useragent
            CURLOPT_URL => $url, // Setting cURL's URL option with the $url variable passed into the function
        );

        $ch = curl_init();  // Initialising cURL 
        curl_setopt_array($ch, $options);   // Setting cURL's options using the previously assigned array data in $options
        $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
        curl_close($ch);    // Closing cURL 
        return $data;   // Returning the data from the function 
    }

    // Defining the basic scraping function
    function scrape_between($data, $start, $end){
        $data = stristr($data, $start); // Stripping all data from before $start
        $data = substr($data, strlen($start));  // Stripping $start
        $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
        $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
        return $data;   // Returning the scraped data from the function
    }
?>

代码:.php

<?php

//include functions
include_once("include/functions.php");

// Set URL
$url = "https://www.instituteforsupplymanagement.org/ismreport/mfgrob.cfm";
$source = curl($url);

// Collect dataset

$arr = array("PMI","New Orders");
    foreach ($arr as $value) {

       $data = scrape_between($source,"<strong>$$value","</tr>");

       print_r($data);

}


?>
PHP 数组 循环 foreach variable-variables

评论

2赞 Rizier123 6/12/2016
“no working” 是什么意思?也许您想在没有美元符号的情况下调用该函数?print_r()
0赞 user6332864 6/12/2016
如果我对$value使用固定值,它将输出正确的信息。但是,当我尝试在变量中使用变量时,输出为零。
1赞 Jeff Puckett 6/12/2016
这里没有变量变量的用法。你至少需要两个美元符号,但你的循环使用一个引用$$value&$value
1赞 Rizier123 6/12/2016
@user6332864 向我们展示您的真实和完整的代码以及您实际(尝试)做什么。
1赞 Rizier123 6/12/2016
如果您只想传递值,则只需使用一个美元符号即可。两个美元符号是不同的东西。

答:

0赞 jclyons52 6/12/2016 #1

在字符串中,第二个 $ 被视为字符串,因此您最终会得到类似 '$PMI' 的字符串。为什么不直接将变量变量分配给临时变量:

$start = $$value;
$data = scrape_between($source,"<strong>$start","</tr>");

我也不知道你打算如何使用“新订单”作为变量名称,空间和所有。 也许您正在尝试在字符串中使用“PMI”和“New Orders”,在这种情况下,只需删除额外的 $