我试图将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;
}