【问题标题】:Extract data from a large and nested JSON file using PHP JsonReader使用 PHP JsonReader 从大型嵌套 JSON 文件中提取数据
【发布时间】:2017-10-07 11:58:19
【问题描述】:

我正在尝试使用 PHP JsonReader 从一个大型嵌套 JSON 文件中提取数据,并将其放入 .csv。我的问题是:

  1. 打印列中以foo_ 开头的键
  2. 打印出嵌套文件中的其他对象值

我想要的 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();
  ?>

【问题讨论】:

    标签: php json csv


    【解决方案1】:

    我已经使用dummy jsonfile 测试了下面的代码,它完美地回答了我发布的问题。

     <?php
     require_once 'C:/xampp/htdocs/json/pcrov/vendor/autoload.php'; //make sure 
     that this is a correct file path
     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, uhc".PHP_EOL);
     {
         if (preg_match('/^foo_/', $reader->name())){ //To retreive keys that 
     // starts with `foo_`, wildcard is employed here using regular expression 
     // function in php. 
        $companyID = $reader->name();
        $companyID1 = str_ireplace("foo_","",$companyID); // this line is 
     // necessary in order to remove foo_ because what is needed in the output 
     // is everything after `foo_`.
    
        if ($reader->read("entityName")){
            $entityName = $reader->value();
            $entityName = str_ireplace(","," ",$entityName); //comma need to be 
     // replaced because csv treats comma as a delimiter. if comma not removed, 
     // csv pushes everything after comma to another column
        }
    
        if ($reader->read("ultimateHoldingCompany")){       
            $uhcName = null;
            $uhc = $reader->value();
            if (empty($uhc)){
            }
            else {
                $uhcName = $uhc[0]['name'];
                $uhcName = str_ireplace(","," ",$uhcName); //comma need to be 
     // replaced because csv treats comma as a delimiter. if comma not removed, 
     // csv pushes everything after comma to another column
            }
    
        }       
    
        fputs($fo,  $companyID1.",".$entityName.",".$uhcName.PHP_EOL);
        }   
      }
     $reader->close();
     ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-30
      • 2015-04-22
      • 1970-01-01
      • 2021-06-10
      • 2019-08-25
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多