0

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 1

edit 2 :

i expect the output something like that

2

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
jassim mishal
  • 73
  • 2
  • 12
  • It sounds like you need to test if `is_array()`, and if so, then to loop over those, ie. `if(is_array($fields)){ foreach($fields as $fields2) { fputcsv($fp, $fields2); } else {fputcsv($fp, $fields); }` – Sean Dec 12 '15 at 19:55
  • posibly like this : https://stackoverflow.com/a/28323742/4262684 ? – cwps Dec 12 '15 at 20:57
  • I've following the link and change my code as below `$fp = fopen("output.csv","w"); foreach ($json as $fields) { $iresult = []; array_walk_recursive($fields, function($item) use (&$iresult) { $iresult[] = $item; }); fputcsv($fp, $iresult); } ` now is working but they all in one line ,, i guess need little edit – jassim mishal Dec 12 '15 at 22:19

1 Answers1

0

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);
trincot
  • 317,000
  • 35
  • 244
  • 286
  • thank you for your effort , actually I've added the first code but i got like warning `PHP Warning: fputcsv() expects parameter 2 to be array, string given in /var/www/mycall.php on line 36` but is writing in output.csv file only the image path either small & big ... for the second code i got error `PHP Warning: fputcsv() expects parameter 2 to be array, string given in /var/www/mycall.php on line 37` with nothing in output.csv file.. I also wondering why i need to put test array for arrays image small & big , since it should be their ? – jassim mishal Dec 12 '15 at 21:41
  • seems is working but still face little issue in the output of link pictures ... each link of pictures is have one row so looks like bad , need to make each group of pics on one row. – jassim mishal Dec 12 '15 at 22:57
  • You mean column, not row, right? Can you provide the expected output in your question? – trincot Dec 12 '15 at 23:26
  • Do you need all image urls, or would the first one be enough. Because putting them together in one column.... I am not sure how you would format that. – trincot Dec 12 '15 at 23:30
  • not all images urls, mean each array have group for image urls.. i have update my question and add the output pic – jassim mishal Dec 12 '15 at 23:39
  • That seems to be the output like my answer would produce: I see three columns, each with an image URL. I don't understand what you want. – trincot Dec 12 '15 at 23:45