PHP将文件传递给recursiveArrayIterator

时间:2014-01-08 00:12:05

标签: php json

有没有办法将文件路径传递给RecursiveArrayIterator,如

 //new RecursiveArrayIterator(json_decode("array from file ", TRUE)),

我知道我可以用以下文件阅读:

           $fileSPLObjects  = new RecursiveIteratorIterator(
           new RecursiveDirectoryIterator($path), 
           RecursiveIteratorIterator::CHILD_FIRST);

这将允许我从文件中读取JSON数组,而不是在页面上定义它。 那么基本上有没有办法将这两个代码片段组合在一起?

 $jsonIterator = new RecursiveIteratorIterator(
 new RecursiveArrayIterator(json_decode($json, TRUE)),
 RecursiveIteratorIterator::SELF_FIRST);

1 个答案:

答案 0 :(得分:1)

如果将尽可能多的代码行折叠成一行,您就不会获得奖励积分。为什么不使用这样的东西?

$json = file_get_contents($json_file_path);
$json_array = json_decode($json, true);
if (is_null($json_array)) {
    // json decode failed. Error handling here
} else {
   $iterator = new RecursiveArrayIterator($json_array);
}