【发布时间】:2017-10-07 11:58:19
【问题描述】:
我正在尝试使用 PHP JsonReader 从一个大型嵌套 JSON 文件中提取数据,并将其放入 .csv。我的问题是:
- 打印列中以
foo_开头的键 - 打印出嵌套文件中的其他对象值
我想要的 CSV 表是 this。不知何故,我没有得到想要的结果,但我知道我有一个想法。这里是my dummy file。
这是我的代码:
<?php
require_once 'C:/xampp/htdocs/json/pcrov/vendor/autoload.php';
use \pcrov\JsonReader\JsonReader;
ini_set("max_execution_time", 0);
$reader = new JsonReader();
$reader->open("jsonfile.json");
$fo = fopen("csvfile.csv", "w" );
fputs($fo, "name, companyID, ultimateHoldingCompany".PHP_EOL);
while ($reader->read(strpos($key, "foo__"))) {
// I want to loop through the key that contains foo_ and print the key name
$companyID = null;
$entityName = null;
$uhc = null;
$companyID = $key
$entityName = $reader->value();
$UltimateHoldingCompany = $reader['ultimateHoldingCompany']['name']-
>value;
fputs($fo,
$entityName.",".$companyID.",".$UltimateHoldingCompany.PHP_EOL);
}
$reader->close();
?>
【问题讨论】: