skip to Main Content

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
                                                (
                                                    January 25, 2018 => 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
                                                (
                                                    January 25, 2018 => 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!

2

Answers


  1. 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];
       }
    }
    
    Login or Signup to reply.
  2. Beautify your json data here to understand how it mapped.

    https://jsonformatter.org/json-editor

    And use array_column() to get your work done!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search