2

I have this array extract from Facebook Graph Api Json.

(array is cutted because I get error for much code)

 Array
(
    [0] => stdClass Object
        (
            [name] => My Account
            [account_id] => 534543534543534534
            [campaigns] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 6786546546546
                        )

                )

            [leadgen_forms] => Array
                (
                    [0] => stdClass Object
                        (
                            [leads] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [created_time] => stdClass Object
                                                (
                                                    [date] => 2018-01-25 06:53:31
                                                    [timezone_type] => 1
                                                    [timezone] => +00:00
                                                )

                                            [id] => 546546546456453453
                                            [field_data] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [name] => full_name
                                                            [values] => Array
                                                                (
                                                                    [0] => John Doe
                                                                )

                                                        )

                                                    [1] => stdClass Object
                                                        (
                                                            [name] => email
                                                            [values] => Array
                                                                (
                                                                    [0] => [email protected]
                                                                )

                                                        )

                                                    [2] => stdClass Object
                                                        (
                                                            [name] => phone_number
                                                            [values] => Array
                                                                (
                                                                    [0] => +39342423423423234
                                                                )

                                                        )

                                                    [3] => stdClass Object
                                                        (
                                                            [name] => post_code
                                                            [values] => Array
                                                                (
                                                                    [0] => 18916
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [id] => 23423423423432423
                        )

                    [1] => stdClass Object
                        (
                            [leads] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [created_time] => stdClass Object
                                                (
                                                    [date] => 2017-12-29 12:11:49
                                                    [timezone_type] => 1
                                                    [timezone] => +00:00
                                                )

                                            [id] => 23423423423423423
                                            [field_data] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [name] => email
                                                            [values] => Array
                                                                (
                                                                    [0] => [email protected]
                                                                )

                                                        )

                                                    [1] => stdClass Object
                                                        (
                                                            [name] => phone_number
                                                            [values] => Array
                                                                (
                                                                    [0] => +3923423423423
                                                                )

                                                        )

                                                    [2] => stdClass Object
                                                        (
                                                            [name] => post_code
                                                            [values] => Array
                                                                (
                                                                    [0] => 81666
                                                                )

                                                        )

                                                    [3] => stdClass Object
                                                        (
                                                            [name] => full_name
                                                            [values] => Array
                                                                (
                                                                    [0] => Marika Doe
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [id] => 335264990275802
                        )
                   )

            [id] => act_129367037453298
        )

)

This is my 'foreach code', but I cannot understand make 'foreach' for get 'John Doe full name' from array.

I know that I should be make another 'foreach' into first 'foreach' but I no found solution.

 $resultArray = json_decode($graphEdge, true);

    foreach($someObject as $datum){
        echo $datum->name;
        echo "<br>";
         foreach($datumas $values){
               echo $values->name;
          }

    }

Can anyone help? Thanks!

3
  • 3
    foreach($datumas $values){ is faulty, missing a space Commented Jan 25, 2018 at 9:32
  • Yes, sorry, Bad copy and paste. Commented Jan 25, 2018 at 9:36
  • from your print, are you sure you used true as second argument from json_decode? It seems as the print come from a json_decode($mydata, false) Commented Jan 25, 2018 at 9:41

2 Answers 2

2

You need to use two foreach() like below:-

foreach($resultArray[0]->leadgen_forms as $datum){
   foreach($datum->leads[0]->field_data as $dt){
        echo $dt->name.'='.$dt->values[0];
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Beautify your json data here to understand how it mapped.

https://jsonformatter.org/json-editor

And use array_column() to get your work done!

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.