如何使用拆分数组拆分表列


How to split a table column using split-array

我目前正在寻找一种方法来分割数据库发送消息后60分钟左右。我有一个表,超过1000行从数据库返回,并希望将它们分成50组。如果能举例说明如何做到这一点,我将不胜感激。谢谢你。

您可以创建一个脚本,它的导航方式如下:

runner.php?start=0&limit=50

本页将从第0行开始,重复50次。然后在重复50次之后,它会回显:

<script>setTimeout(function() {
    window.location='runner.php?start=50&limit=50';
}, 3600000)</script>

其中start=50$_GET['start'] + $_GET['limit']

你需要保持你的浏览器窗口打开。

您可以使用array_chunk:

$chunks = array_chunk($results, 50);
foreach ($chunks as $i => $chunk) {
    if ($i != 0) {
        sleep(3600); // Pause an hour except at the beginning
    }
    foreach ($chunk as $row) {
        // Do what you want with the row
    }
}

不要在网页上这样做,因为浏览器会超时等待结果。但是您可以在CLI脚本中完成。