1

How can i replace/remove some string in this JSON ?.I think this problem can be solve using str_replace method or preg_replace but i don't know how to add the regex

Please help me.

here my json

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
)

will i use this function to export the json to csv

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

the above code work fine but each image link is have one column so looks like bad , i need to make each group of pics on one column I try to add regex to part of link images except the first image url [0] and merge the other with it , that i can putting them together in one column.... so for the experiment i add this to the above code but seems nothing happen

 $flat[$k] = str_replace('[1-7] => http', "http", $v); 

here i expect the output something like that

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

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

edit this the .csv file output look like this

this

and I'm looking to be something like that

this the s

5
  • change $flat[$k] = $v; to $flat[$k] = is_array($v)?implode(' ',$v):$v;, if i get you right Commented Dec 22, 2015 at 23:15
  • I've add but does do any change Commented Dec 22, 2015 at 23:21
  • i think i dont get will i use this function to export the json to csv , because you do open and csv and convert it in your code? Commented Dec 22, 2015 at 23:24
  • may add the unexpect outptut Commented Dec 22, 2015 at 23:25
  • I've edit my question and add the screenshot for csv file output and the expect , btw I've not put the complete code but the json data i got it from online source then decode it and convert it to csv. Commented Dec 22, 2015 at 23:43

1 Answer 1

1

ok I've fixed by

foreach (
    new RecursiveArrayIterator($nonFlat) as $k=>$v) {
 $flat[$k] = is_array($v)?implode(" ",$v):$v;

now i got each group of images on one column

thanks :)

Sign up to request clarification or add additional context in comments.

Comments

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.