首页 > 解决方案 > 如何在 PHP 中取消对关联数组的排序?

问题描述

我想检查域的状态代码,为了不给远程服务器带来太多负担,我想确保请求之间有足够的时间。

为此,我需要对关联数组(id:url)和

生成这样的队列:域 a,域 b,c,a,b,c,a,b,c,a...

我尝试了以下操作,循环遍历域并检查在最后 3 次迭代中是否请求了该域,继续下一个域并将这个域置于循环的末尾。

$urls_last_checked = array();
foreach ($check as $id => $url) {

    // let's only monitor the last x urls with processed
    // save domain to array in order to avoid to many requests to this URL in a short period of time
    $domain = parse_url($url, PHP_URL_HOST);
    // only continue with this domain if not checked before
    if (in_array($domain, $urls_last_checked)){ // we processed it before continue with next one
        echoerror ($domain." passed \n", $logfile);
        unset($check[$id]);
        $check[$id] = $url;
        continue;
    }
    else{
        // add this domain to array
        $urls_last_checked[] = $domain;
        // remove oldest domain we checked
        if (count($urls_last_checked) > 3)  array_shift($urls_last_checked);
        echoerror (print_r($urls_last_checked).$domain." added \n", $logfile);
    }
}

不幸的是,这不起作用,因为 for 循环将结束并且不考虑我添加的新数组元素。

如何确保前循环继续进行或以不同的方式实现我的目标?

标签: phparrays

解决方案


推荐阅读