php 嵌套 for 语句?

发布于 2024-09-05 03:40:35 字数 242 浏览 2 评论 0原文

我正在尝试在 for 循环中处理 for 循环,并且对语法有点警惕......这行得通吗?本质上,我想在计数等于或小于 $count 时为每 1,000 条记录运行代码...下面的语法是否有效,或者是否有更好的方法?

for($x = 0; $x <= 700000; $x++) {
  for($i = 0; $i <= 1000; $i++) {
     //run the code
  }
} 

I'm trying to process a for loop within a for loop, and just a little wary of the syntax... Will this work? Essentially, I want to run code for every 1,000 records while the count is equal to or less than the $count... Will the syntax below work, or is there a better way?

for($x = 0; $x <= 700000; $x++) {
  for($i = 0; $i <= 1000; $i++) {
     //run the code
  }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぺ禁宫浮华殁 2024-09-12 03:40:35

您拥有的语法将起作用,但我认为它不会完全满足您的要求。现在,它将执行外循环 700,001 次,而对于这 700,001 次中的每一次,它将执行内循环。

这意味着内部循环总共将运行 700,001 x 1001 = 大约 700.7 百万次。

如果这不是您想要的,您能提供更多信息吗?我无法真正弄清楚“当计数等于或小于 $count 时,我想为每 1,000 条记录运行代码”是什么意思。我根本没有看到任何名为 $count 的变量。

The syntax you have will work, but I don't think it's going to do exactly what you want. Right now, it's going to do the outer loop 700,001 times, and for every single one of those 700,001 times, it's going to do the inner loop.

That means, in total, the inner loop is going to run 700,001 x 1001 = about 700.7 million times.

If this isn't what you want, can you give a bit more information? I can't really work out what "I want to run code for every 1,000 records while the count is equal to or less than the $count" means. I don't see any variable named $count at all.

冰雪之触 2024-09-12 03:40:35

嗯,本质上,我正在读取一个文本文件并将每一行插入到数据库中。我最初确实尝试过 while(!feof($f)) [其中 $f = 文件名],但它一直抱怨管道损坏。我认为这将是另一种方式

$f 应该是 fopen() 返回的文件句柄,而不是文件名。

$file_handle = fopen($filename, 'r');

while(!feof($file_handle)) {
    $line = fgets($file_handle);

    $line = trim($line); // remove space chars at beginning and end

    if(!$line) continue; // we don't need empty lines

    mysql_query('INSERT INTO table (column) '
               .'VALUES ("'.mysql_real_escape_string($line).'")');
}

通读 php.net 上的 fopen()、fgets() 文档。如果您需要拆分字符串,您可能还需要explode()。

如果您的文件不大,您可能希望立即将其读入数组,如下所示:

$filelines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($filelines as $line) {
   do_stuff_with($line);
}

Well, essentially, I'm reading in a text file and inserting each of the lines into a db. I did originally try while(!feof($f)) [where $f = filename], but it keeps complaining of a broken pipe. I thought this would be another way to go

$f should be file-handle returned by fopen(), not a filename.

$file_handle = fopen($filename, 'r');

while(!feof($file_handle)) {
    $line = fgets($file_handle);

    $line = trim($line); // remove space chars at beginning and end

    if(!$line) continue; // we don't need empty lines

    mysql_query('INSERT INTO table (column) '
               .'VALUES ("'.mysql_real_escape_string($line).'")');
}

Read through the documentation at php.net for fopen(), fgets(). You might also need explode() if you need to split your string.

If your file isn't big, you might want to read it into an array at once like this:

$filelines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($filelines as $line) {
   do_stuff_with($line);
}
伴梦长久 2024-09-12 03:40:35

嗯。好吧......好吧,本质上,我正在读取一个文本文件并将每一行插入到数据库中。我最初确实尝试过 while(!feof($f)) [其中 $f = 文件名],但它一直抱怨管道损坏。我认为这将是另一种方式......

要逐行读取文本文件,我通常:

$file = file("path to file")
foreach($file as $line){
 //insert $line into db
}

Hmm. Ok... Well, essentially, I'm reading in a text file and inserting each of the lines into a db. I did originally try while(!feof($f)) [where $f = filename], but it keeps complaining of a broken pipe. I thought this would be another way to go..

To read a text file line by line I usually:

$file = file("path to file")
foreach($file as $line){
 //insert $line into db
}
静待花开 2024-09-12 03:40:35

严格回答这个问题,您会想要更像这样的东西:

// $x would be 0, then 1000, then 2000, then 3000
for($x = 0; $x < 700000; $x += 1000) {
  // $i would be $x through $x + 999
  for($i = $x; $i < $x + 1000; $i++) {
     //run the code
  }
}

但是,您确实应该考虑将文件导入数据库的其他方法之一。

Strictly answering the question, you'd want something more like this:

// $x would be 0, then 1000, then 2000, then 3000
for($x = 0; $x < 700000; $x += 1000) {
  // $i would be $x through $x + 999
  for($i = $x; $i < $x + 1000; $i++) {
     //run the code
  }
}

However, you should really consider one of the other methods for importing files to a database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文