php - 如何在 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 循环将结束并且不考虑我添加的新数组元素。
如何确保前循环继续进行或以不同的方式实现我的目标?
解决方案
推荐阅读
- javascript - 使用超时来实现跳转功能
- ruby - 试图剔除一个集合,但集合不断消失
- javascript - 为什么必须在 componentDidMount() 期间解析函数中的函数调用?
- wpf - 当目标应用程序移动到第二台显示器时,为什么我的应用程序包含 DWM 缩略图滞后?
- opengl - 无法用鼠标选择在帧缓冲区中渲染的四边形
- python - 具有 **kwargs 输出的类:未定义 arg
- python - 我可以在识别段落中的给定单词时使用自然语言处理还是需要使用机器学习算法
- mysql - 如何计算查询结果
- php - Laravel:无法打开蒸汽:没有这样的文件或目录
- java - 我有错误:“javac.exe”以代码 3 退出