【问题标题】:PHP using for inside forPHP 在 for 中使用 for
【发布时间】:2018-11-19 10:10:36
【问题描述】:

有人可以帮助我使用随机或顺序 api 列表编写执行数据的代码

我有 10 个数据和 3 个 api 列表,我希望代码这样做:

data 1  executed by api 1
data 2  executed by api 2
data 3  executed by api 3
data 4  executed by api 1
data 5  executed by api 2
data 6  executed by api 3
data 7  executed by api 1
data 8  executed by api 2
data 9  executed by api 3
data 10 executed by api 1

我尝试像这样编写代码,结果与我预期的不一样:

if(isset($argv[1])){
$no = 1;
$api = 0;
$total = count(explode("\n",file_get_contents($argv[1])));
$listapi = file_get_contents($argv[2]);
$listml = file_get_contents($argv[1]);
$ex = explode("\n",$listapi);
$exx = explode("\n",$listml);
for ($ee = 0; $ee < count($exx); $ee++) {
    for ($api = -1; $api < count($listapi); $api++) {
    $send = json_decode($curl->get($ex[$api]."?ee=".$exx[$ee]));
    $no++;
    echo "OK\n";
    if ($api % count($ex) == 3) {
        $api = -1;
        echo "Rolling\n";
        }
    }
}

【问题讨论】:

    标签: php loops random


    【解决方案1】:

    您需要循环遍历data数组在api数组中找到相关项,如下所示。

    foreach ($data as $key=>$item){
        echo "data $item executed by api ".$api[$key%count($api)]."\n";
    }
    

    demo查看结果

    【讨论】:

    • 但是如果api和数据使用的是count而不是range怎么办?
    • @SmithGeorge 数据正在使用计数是什么意思?也许3v4l.org/2OpXk
    • 我的意思是像 $data = count("datalist.txt");
    • @SmithGeorge count() 返回整数而不是数组。
    猜你喜欢
    • 1970-01-01
    • 2014-07-09
    • 2012-10-20
    • 2015-01-28
    • 2019-01-06
    • 1970-01-01
    • 2016-09-05
    • 2014-11-24
    • 2012-09-30
    相关资源
    最近更新 更多