在更改变量名称时循环

时间:2018-02-12 23:12:58

标签: php facebook loops facebook-graph-api

使用FaceBook PHP SDK,当我想获得下一页结果时,我会调用$ next = $ fb-> next($ response)之类的内容。现在下一页,我将不得不拨打$ next2 = $ fb-> next($ next)。我想把它放到一个循环中,显然,在我完成所有结果页面之前,我将永远不知道执行多少次。当没有更多页面时,$ fb-> next()调用将= null。

这是一个没有循环的例子:

$reactions = array();
$response = $response->getGraphEdge();

foreach ($response as $reaction) {
    $reactions[] = $reaction['name'] . " - " . $reaction['type'];
}

$next = $fb->next($response);
foreach ($next as $reaction) {
    $reactions[] = $reaction['name'] . " - " . $reaction['type'];
}

$next2 = $fb->next($next);
foreach ($next2 as $reaction) {
    $reactions[] = $reaction['name'] . " - " . $reaction['type'];
}

$next3 = $fb->next($next2);
foreach ($next2 as $reaction) {
    $reactions[] = $reaction['name'] . " - " . $reaction['type'];
}

print_r($reactions);

1 个答案:

答案 0 :(得分:0)

我过于复杂了..我没有考虑到如果我保持变量名相同的事实,它会在循环的每次迭代中得到更新。所以这很有效:

                $reactions = array();
                $response = $response->getGraphEdge();

                if ($fb->next($response) == null) {

                    foreach ($response as $reaction) {
                        $reactions[] = $reaction['name'] . " - " . $reaction['type'];
                    }

                }else {

                    foreach ($response as $reaction) {
                        $reactions[] = $reaction['name'] . " - " . $reaction['type'];
                    }

                    while ($response = $fb->next($response)) {

                        foreach ($response as $reaction) {
                            $reactions[] = $reaction['name'] . " - " . $reaction['type'];
                        }

                    }

                }

                echo "<pre>";
                print_r($reactions);
                echo "</pre>";