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

3
  • 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); } Commented Dec 12, 2015 at 19:55
  • posibly like this : stackoverflow.com/a/28323742/4262684 ? Commented Dec 12, 2015 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 Commented Dec 12, 2015 at 22:19

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

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 ?
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.
You mean column, not row, right? Can you provide the expected output in your question?
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.
not all images urls, mean each array have group for image urls.. i have update my question and add the output pic
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.