eval()与file_get_contents()..不同的结果

我试图将eval()与json_decode(file_get_contents())一起使用,并在评估和硬编码值时得到似乎不同的结果。我只是缺少一些简单的东西吗?

我正在使用PHP版本5.3.24,目前还不能更改。如果对eval()的支持至关重要,那么我可以开始进行更改的过程。我没有发现任何暗示我在当前的PHP实现中没有eval()支持的东西。

当我运行Example1('key')时,我从eval()函数返回了NULL。当我运行Example2('key')时,我会返回一个基于json数据的数组,如下所示:

{ key_list: [ { data1_list: [ { subkey1: "data",subkey2: 0 },.. ],.. ] }

这是Example1():

function Example1($key) {

    $endPoint = 'http:'.'//some.website.com/json/'.$key;

    $evalCommand = sprintf('json_decode(file_get_contents("%s"))->%s_list[0];',$endPoint,$key);

    echo '$evalCommand = |'.$evalCommand.'|<br />';

    $resultsArray = eval($evalCommand);

    return $resultsArray;
}

这是Example2():

function Example2($key) {

    $endPoint = 'http:'.'//some.website.com/json/'.$key;

    $resultsArray = json_decode(file_get_contents($endPoint))->key_list[0];

    return $resultsArray;
}
coder002 回答:eval()与file_get_contents()..不同的结果

与其使用eval,还可以使用更多标准代码并使用动态对象访问。

URL的名称可以,但是对于该字段来说有些杂乱。这在$field中设置了名称,但是我无法让它在一行中完成该部分和数组部分。在这里它使用->$field来获取数据,而[0]在返回值上。

function Example2($key) {
    $endPoint = 'http://some.website.com/json/'.$key;

    $field = $key.'_list';
    $resultsArray = json_decode(file_get_contents($endPoint))->$field;

    return $resultsArray[0];
}
本文链接:https://www.f2er.com/3019226.html

大家都在问