具有多维数组的json到csv中,其中子元素也具有父级值

时间:2019-04-18 07:45:22

标签: php arrays json csv

我正在尝试将json文件解析为csv,当我遇到带有parent元素的多维数组时会发生问题-我需要该parent数组的每个子元素都必须是csv中带有parent值的单独行

当我尝试循环遍历子元素并且无法访问父级值时,就会出现问题-我正在考虑在循环数据之前制作紧缩的字符串,然后为每个值打印字符串+数据,但这似乎是骇客。有谁知道解决这个问题的更好方法?

  [0]=>
  array(7) {
    ["platform"]=>
    string(7) "web"
    ["date"]=>
    string(10) "2019-04-17"
    ["name"]=>
    string(42) "something.com"
    ["data"]=>
    array(10) {
      [0]=>
      array(2) {
        ["revenue"]=>
        float(0.02)
        ["countryCode"]=>
        string(2) "AU"
      }
      [1]=>
      array(2) {
        ["revenue"]=>
        float(0.03)
        ["countryCode"]=>
        string(2) "BE"
      }
      [2]=>
      array(2) {
        ["revenue"]=>
        float(0.02)
        ["countryCode"]=>
        string(2) "CH"
      }
      [3]=>
      array(2) {
        ["revenue"]=>
        float(0.01)
        ["countryCode"]=>
        string(2) "CZ"
      }
      [4]=>
      array(2) {
        ["revenue"]=>
        float(0.34)
        ["countryCode"]=>
        string(2) "GB"
      }
      [5]=>
      array(2) {
        ["revenue"]=>
        float(0.03)
        ["countryCode"]=>
        string(2) "NL"
      }
      [6]=>
      array(2) {
        ["revenue"]=>
        float(0.01)
        ["countryCode"]=>
        string(2) "NO"
      }
      [7]=>
      array(2) {
        ["revenue"]=>
        float(0.01)
        ["countryCode"]=>
        string(2) "NZ"
      }
      [8]=>
      array(2) {
        ["revenue"]=>
        float(0.03)
        ["countryCode"]=>
        string(2) "SE"
      }
      [9]=>
      array(2) {
        ["revenue"]=>
        float(2.46)
        ["countryCode"]=>
        string(2) "US"
      }
    }
  }
[1]=>
  array(7) {
    ["platform"]=>
    string(7) "web"
    ["date"]=>
    string(10) "2019-04-17"
    ["name"]=>
    string(42) "something-else.com"
    ["data"]=>
    array(10) {
      [0]=>
      array(2) {
        ["revenue"]=>
        float(0.72)
        ["countryCode"]=>
        string(2) "AU"
      }
      [1]=>
      array(2) {
        ["revenue"]=>
        float(12.03)
        ["countryCode"]=>
        string(2) "BE"
      }
      [2]=>
      array(2) {
        ["revenue"]=>
        float(0.27)
        ["countryCode"]=>
        string(2) "CH"
      }
      [3]=>
      array(2) {
        ["revenue"]=>
        float(8.71)
        ["countryCode"]=>
        string(2) "CZ"
      }
    }
  }

我希望将其转换为:

platform,date,name,revenue,countryCode
"web","2019-04-17","something.com",0.02,"AU"
"web","2019-04-17","something.com",0.03,"BE"
"web","2019-04-17","something.com",0.02,"CH"
"web","2019-04-17","something.com",0.01,"CZ"
"web","2019-04-17","something.com",0.34,"GB"
...

设法做到这一点:

            $f = fopen('examlple.csv', 'w');

            $firstLineKeys = false;

            foreach ($array as $line)
            {
                if (empty($firstLineKeys))
                {
                    $firstLineKeys = array_keys($line);
                            array_pop($firstLineKeys); //removing data as we will loop over it later, and adding the 2 data keys that will be in the results.
                            $firstLineKeys[] = "country";
                            $firstLineKeys[] = "revenue";
                    fputcsv($f, $firstLineKeys);
                    $firstLineKeys = array_flip($firstLineKeys);
                }
                $line_array = array($line);
                $string = array();
                    foreach ($line as $key => $value)
                {
                        if($key !== "data"){
                            $string[]= $value;
                        }else{
                            foreach($value as $country){
                            $new_string = $string;
                            $new_string[]=$country['countryCode'];
                            $new_string[]=$country['revenue'];
                            fputcsv($f, $new_string);
                            }
                        }
                        array_push($line_array,$value);
                }
            }

但是我想知道是否还有更好的方法。

1 个答案:

答案 0 :(得分:0)

我已经创建了如下数组,请检查它是否对您有帮助:

        $final = array();
        $i = 0;
        $f = array();
        echo '<pre>';
        foreach($arr as $ar) {
            $final[$i] = array(
                'platform' => $ar['platform'],
                'date' => $ar['date'],
                'name' => $ar['name'],
            );                
            foreach($ar['data'] as $key => $value) {                    
                $f[] = array_merge($final[$i], $value);
            }                
            $i++;
        }
        print_r($f);
        die;

示例:

$arr = array(
            array(
                'platform' => 'web',
                'date' => 'web',
                'name' => 'web',
                'data' => array(
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU',
                    ),
                )
            ),
            array(
                'platform' => 'web1',
                'date' => 'web',
                'name' => 'web',
                'data' => array(
                    array(
                        'revenue' => '0.0211',
                        'countryCode' => 'AU11',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU12',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU13',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU14',
                    ),
                )
            ),
            array(
                'platform' => 'web2',
                'date' => 'web',
                'name' => 'web',
                'data' => array(
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU21',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU22',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU23',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU24',
                    ),
                )
            ),
            array(
                'platform' => 'web3',
                'date' => 'web',
                'name' => 'web',
                'data' => array(
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU31',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU32',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU33',
                    ),
                    array(
                        'revenue' => '0.02',
                        'countryCode' => 'AU34',
                    ),
                )
            )
        );

        $final = array();
        $i = 0;
        $f = array();
        echo '<pre>';
        foreach($arr as $ar) {
            $final[$i] = array(
                'platform' => $ar['platform'],
                'date' => $ar['date'],
                'name' => $ar['name'],
            );                
            foreach($ar['data'] as $key => $value) {                    
                $f[] = array_merge($final[$i], $value);
            }                
            $i++;
        }
        print_r($f);
        die;