PHP convert json into csv with sub array

2019-09-12 09:37发布

问题:

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
)

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);

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) {

with

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) {

edit :

here the output

edit 2 :

i expect the output something like that

回答1:

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;
}

And call it like this:

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


标签: php csv fputcsv