PHP使用子数组将json转换为csv

问题描述:

我正在创建php代码,将以下json转换为csv

I was creating php code to convert the below json to csv

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [DESC] => bla bal bal
                    [SOLD] => 0
                    [contact_no] => 1234
                    [title] =>  Hiiiii
                    [price] => 10900
                    [big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/14.jpg
                            [1] => http://example.com/images/user_adv/15.jpg
                        )

                    [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/14.jpg
                            [1] => http://example.com/images/user_adv/small/15.jpg
                        )

                    [tpe] => user
                )

            [1] => Array
                (
                    [DESC] => fo fo fof ofof
                    [SOLD] => 0
                    [contact_no] => 234522
                    [title] => Hellooooo sddf
                    [price] => 0
                    [big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/154.jpg
                            [1] => http://example.com/images/user_adv/144.jpg
                            [2] => http://example.com/images/user_adv/147.jpg
                        )

                    [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/154.jpg
                            [1] => http://example.com/images/user_adv/small/144.jpg
                            [2] => http://example.com/images/user_adv/small/147.jpg
                        )

                    [tpe] => user
                )

        )

    [pis] => 3
    [totals] => 23
    [curpage] => 1
    [total_ads] => 71
)

我一直在使用以下代码将其导出到.csv

I've been using the below code to export it to .csv

$fp = fopen("output.csv","w");

foreach ($json['data'] as $fields) {
fputcsv($fp, $fields);
}

fclose($fp);

我可以很好地转换它,但是我遇到了一个小问题,即big_image& small_image没有出现在输出文件.csv中(行为空)

I can convert it fine, but I face a small issue that the sub array which is big_image & small_image is NOT appearing in the output file .csv (the row is empty)

       [big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/154.jpg
                            [1] => http://example.com/images/user_adv/144.jpg
                            [2] => http://example.com/images/user_adv/147.jpg
                        )

                    [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/154.jpg
                            [1] => http://example.com/images/user_adv/small/144.jpg
                            [2] => http://example.com/images/user_adv/small/147.jpg
                        )

顺便说一句,如果我替换:

By the way, if I replace:

foreach ($json['data'] as $fields) {

foreach ($json['data'][0] as $fields) {

我将链接图片作为输出,所以我需要将它们合并为一个输出

I get the link pictures as output, so I need to merge them as one output

  foreach ($json['data'] as $fields) {
  foreach ($json['data'][0] as $fields2) {

此处输出

我希望输出是这样的

您可以创建一个函数来确保将嵌套数组放平.您可以为此创建一个实用程序函数,如下所示:

You could make a function to make sure those nested arrays are made flat. You can create a utility function for that, like this:

function array_flatten ($nonFlat) {
    $flat = array();
    foreach (new RecursiveIteratorIterator(
            new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
        $flat[$k] = $v;
    }
    return $flat;
}

并这样称呼它:

$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
    fputcsv($fp, array_flatten($fields));
}
fclose($fp);