$list = array(
"0" => "https://...",
"1" => "http://...",
"2" => "https://...",
"3" => "https://...",
);
/*
* @名称: php ,对数组每隔一定的时间(自设定时间)来轮流进行位置排序,轮询的排行榜。
精确到指定的秒 或 分钟 或 小时 或 天 ,对数据列表进行轮排。
* @参数: (array)$list 顺序数组,传入需要进行轮排的数组;
* @参数: (time string)$polling_time 间隔时间 , 轮排间隔的时间。可以是:数字 + 秒、分、时、天(英文单词);
* @参数: (int)$polling_number 每次轮流换排多少条数据;
* @返回值: array | false , 如果排序成功返回array,否则异常情况返回false.
*
*/
function dataPollingInterval( $list , $polling_time , $polling_number ) {
// 规划轮询间隔时间的参数:
$interval = false;
// 判断$polling_time 的类型是 秒、分、小时、天 中的哪1种。
$arg = array(
's'=>1 , // 秒
'm'=>60 , // 分= 60 sec
'h' =>3600 , // 时= 3600 sec
'd' => 86400 , // 天= 86400 sec
);
// 判断间隔时间的类型,并计算间隔时间
foreach ( $arg as $k => $v ) {
if ( false !== stripos( $polling_time , $k ) ) {
$interval = intval( $polling_time ) * $v;
break;
}
}
// 判断间隔时间
if( !is_int( $interval ) ){
return false;
}
// 从今年开始的秒数
$this_year_begin_second = strtotime( date( 'Y-01-01 01:00:01' , time() ) );
// 当前秒数 - 今年开始的秒数,得到今年到目前为止的秒数。
$polling_time = time() - $this_year_begin_second;
// 从今年到目前为止的秒数,计算得到当前轮数
$len = count( $list ); // 总长度
$start_index = intval( $polling_time / $interval );
$start_index = $polling_number * $start_index % $len; // 轮排数量 * 轮数 , 取余 总数量。
$res = array( );
// 将轮数 指向到数组的索引,然后从这个索引开始接着往下循环遍历。
for ( $i=0; $i < $len ; ++$i ) {
$index = $i + $start_index; // 索引的变化是根据时间来变
// 当遍历索引超过数组的最大下标时,
if ( $index >= $len ) {
$index = $index - $len ;
}
$res[] = $list[ $index ]; // 存入结果
}
return $res;
}
$new_list=dataPollingInterval($list,'3 sec',1);