为foreach()提供的PHP无效参数需要帮助循环

时间:2017-03-04 23:47:51

标签: php arrays foreach

Array (
    [feed] =>
        Array (
            [data] =>
                Array (
                    [0] =>
                        Array (
                            [message] => Work and classes in all levels are suspended again for tomorrow, March 6, 2017
                            [created_time] => 2017-03-04T21:15:46+0000
                            [id] => 209883939491413_209959689483838
                        )
                    [1] =>
                        Array (
                            [message] => Work and classes in all levels are suspended again for tomorrow, March 5, 2017
                            [created_time] => 2017-03-04T21:15:16+0000
                            [id] => 209883939491413_209959609483846
                        )
                    [2] =>
                        Array (
                            [message] => Work and classes in all levels are suspended again for tomorrow, August 8.
                            [created_time] => 2017-03-04T18:17:30+0000
                            [id] => 209883939491413_209884442824696
                        )
                )
            [paging] =>
                Array (
                    [previous] => https://graph.facebook.com/v2.8/209883939491413/feed?since=1488662146&access_token=EAAa5qb2ZBYRABAFSRRwJT8d20poghFzc66qN3PbTb3ZBWG    cV0A32VkdoRYSWscNOKt8p0geBtSUXJPZABUUXuAPOL9EGTBTnn9137HW4gKshYiFj1j22fjYjDduLsgvXjwlCIlqu3Xs5gdEBuzxfa8GYCNEbz7rKZAjzeDCQmwKrI1Al49WWlqXizp    VI1iMZD&limit=25&__paging_token=enc_AdD2Pa1ooXucxzHNJ8uFlImnoaZCJRhup1m2upmP3SiQSu4J6KRkAOm5cVIrnNXgehamKLLH7Ezc5pvT53ZAh7PxY0slfBlJnwRZBvZC    6dFouYzyWgZDZD&__previous=1
                    [next] => https://graph.facebook.com/v2.8/209883939491413/feed?access_token=EAAa5qb2ZBYRABAFSRRwJT8d20poghFzc66qN3PbTb3ZBWGcV0A32VkdoRYSWscNOKt8    p0geBtSUXJPZABUUXuAPOL9EGTBTnn9137HW4gKshYiFj1j22fjYjDduLsgvXjwlCIlqu3Xs5gdEBuzxfa8GYCNEbz7rKZAjzeDCQmwKrI1Al49WWlqXizpVI1iMZD&limit=25&unti    l=1488651450&__paging_token=enc_AdDWpQlc1kmhJKZAZAny9PZAnFEZBIRwQq5MHuGOqGBANpHoGQ7L4ZBXWXCdeUp7UyUxUMr988aqZCXEee9ZCOqclzH9EZBqWVwdQPkWHWUZ    Ayn4AdNpVMwZDZD
                )
        )
    [id] => 209883939491413
)

伙计我有一个数组而且我只打印消息时遇到问题,因为我得到了#34;为foreach()提供了无效的参数"我不知道为什么。

这是我的foreach代码:

foreach ($userNode as $array1 => $array2) {
    echo "array1: $array1 <br>";
    foreach ($array2 as $array3 => $array4) {
        echo "array3: $array3 <br>";
        foreach ($array4 as $array5 => $array6) {
            echo "array5: $array5 <br>";
            foreach ($array6 as $array7 => $array8) {
                if ($array7=="message") {
                    echo "$array7 : " . "$array8 <br>";
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

if (is_array($userNode) || is_object($userNode)) {
 foreach ($userNode as $array1 => $array2) {
    echo "array1: $array1 <br>";
    if (is_array($array2) || is_object($array2)) {
        foreach ($array2 as $array3 => $array4) {
            echo "array3: $array3 <br>";
            if (is_array($array4) || is_object($array4)) {
                foreach ($array4 as $array5 => $array6) {
                    echo "array5: $array5 <br>";
                    if (is_array($array6) || is_object($array6)) {
                        foreach ($array6 as $array7 => $array8) {
                            if ($array7=="message") {
                                echo "$array7 : " . "$array8 <br>";
                            }
                        }
                    }
                }
            }
        }
    }
}}

UPDATE!修复它!

答案 1 :(得分:0)

不确定为什么需要迭代这么多次。我会这样做:

foreach($arr['feed']['data'] as $data){
    print_r($data['message']);
}