如何重复foreach

时间:2016-10-01 11:11:38

标签: php

这是代码:

$dat = getContents($id, $session);
foreach($dat['data']['followings'] as $gg){
    $fid = $gg['fid'];
    $f = follow($fid, $session, $atok);
    echo $f['data']['credits'] . "\n";
}

当我运行此脚本时,我的硬币会得到结果

例如:

11 12

那么,如何重复代码直到我得到50硬币或100或类似的东西?

因为在PHP上只给我2个结果。我可以使它达到10或50或100,但我可以使用COPY PASTE这些脚本。你知道吗?

这是我重复此代码的方式:

$dat = getContents($id, $session);
foreach($dat['data']['followings'] as $gg){
    $fid = $gg['fid'];
    $f = follow($fid, $session, $atok);
    echo $f['data']['credits'] . "\n";
} 

$dat = getContents($id, $session);
foreach($dat['data']['followings'] as $gg){
    $fid = $gg['fid'];
    $f = follow($fid, $session, $atok);
    echo $f['data']['credits'] . "\n";
}

$dat = getContents($id, $session);
foreach($dat['data']['followings'] as $gg){
    $fid = $gg['fid'];
    $f = follow($fid, $session, $atok);
    echo $f['data']['credits'] . "\n";
}

所以我可以得到6个结果。我的问题是如何制作代码而不复制粘贴此代码。

3 个答案:

答案 0 :(得分:1)

Foreach是一个读取数组所有索引的循环,它会重复自身,直到读取该数组的所有索引。

答案 1 :(得分:0)

for($i = 0; $i < 10; $i++){
    $dat = getContents($id, $session);
    foreach ($dat['data']['followings'] as $gg){
        $fid = $gg['fid'];
        $f = follow($fid, $session, $atok);
        echo $f['data']['credits'] . "\n";
    }
}

答案 2 :(得分:0)

$dat = getContents($id, $session);
$counter = 0;
while ($counter < 100) {
    foreach ($dat['data']['followings'] as $gg){
        $fid = $gg['fid'];
        $f = follow($fid, $session, $atok);
        $counter++
        echo $f['data']['credits'] . "\n";
    }
}