打印嵌套数组PHP

时间:2018-01-25 09:30:49

标签: php arrays json foreach nested

我有来自Facebook Graph Api Json的这个数组摘录。

(数组因为我的代码错误而被切断)

 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] => johndoe@hotmail.com
                                                                )

                                                        )

                                                    [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] => marikadoe@hotmail.com
                                                                )

                                                        )

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

)

这是我的'foreach代码',但是我无法理解make'foreach'是为了从数组中获取'John Doe全名'。

我知道我应该在第一个'foreach'中制作另一个'foreach',但我找不到解决方案。

 $resultArray = json_decode($graphEdge, true);

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

    }

有人可以帮忙吗?谢谢!

2 个答案:

答案 0 :(得分:2)

您需要使用以下两个foreach(): -

foreach($resultArray[0]->leadgen_forms as $datum){
   foreach($datum->leads[0]->field_data as $dt){
        echo $dt->name.'='.$dt->values[0];
   }
}

答案 1 :(得分:1)

在此处美化您的json数据以了解它的映射方式。

https://jsonformatter.org/json-editor

使用 array_column()来完成工作!