dongxia9519 2014-03-10 15:44
浏览 42
已采纳

php一次处理50个数组元素并在完成后删除它们

I have an array of 100 links (each array element is a link) and I want to process to 5 at a time. I was thinking of using array_slice() to achieve this however once the first 5 elements are processed I want to remove them from my array so only the unprocessed links to remain in it until all the elements are processes and array becomes empty.

My code looks like this:

$links = array("http://www.example.com", "http://www.example.com", "http://www.example.com", "http://www.example.com", "http://www.example.com", "http://www.example.com", "http://www.example.com", "http://www.example.com", "http://www.example.com");

$first_five = array_slice($links, 0, 5);

foreach($first_five as $ff) {
process_link($ff);
}

// so far so good but how do I remove the processed elements and process the remaining onces until $links[] is empty? ?>

  • 写回答

2条回答 默认 最新

  • dousong1926 2014-03-10 15:49
    关注

    array_chunk() might be what you're looking for. It splits the given array into an array of arrays, each containing the specified number of elements:

    foreach (array_chunk($links, 5) as $slice) {
        // $slice is an array containing 5 elements
        // process it further
    }
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥50 用AT89C52单片机设计一个温度测量与控制电路
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题