【问题标题】:Looping over results and changing a parameter after each loop循环结果并在每次循环后更改参数
【发布时间】:2015-08-17 22:01:17
【问题描述】:

我正在使用 API,我需要收集所有公司(大约 500 家)。现在 API 规定每页的最大请求量为 100

现在我想知道如何循环这些结果,同时在 100 个结果之后增加页码。我现在有以下内容,但它没有考虑到页面。

// 0 is the page number, this should increment after each iteration and then I should run this function 
$companies = API::getCompanies(100, 0);

foreach($companies as $company) {
    // do stuff here.
}

未知数量的结果

公司的数量应该是动态的,所以不知道结果有多少页,我在想用while算子看看还有没有数据进来?这是要走的路吗?

谢谢

【问题讨论】:

    标签: php logic


    【解决方案1】:

    这将获取所有公司。

    $page = 0;
    while(true) {
        $companies = API::getCompanies(100, $page++);
    
        // Here i assume that when you get to the end of the list
        // the API should return an empty array/error message/exception etc ...
        // so check if this condition is met and finish the loop.
    
        if (!$companies) break;
        foreach($companies as $company) {
            // do stuff here.
        }
    }
    

    【讨论】:

    • 谢谢你的回答,我忘了补充公司的数量是动态的..所以应该不知道有多少..
    猜你喜欢
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2019-11-10
    相关资源
    最近更新 更多