PHP:foreach限制999结果

时间:2014-02-18 11:20:49

标签: php foreach

foreach(array_slice(glob('/res/images/*.jpg'), 0, 999) as $filename)

工作正常,但

foreach(array_slice(glob('/res/images/*.jpg'), 0, 1000) as $filename)

不起作用。我可以在哪里改变这种限制?

3 个答案:

答案 0 :(得分:3)

尝试简单(可能更好)的方式,如

$i = 0;
foreach(glob('/res/images/*.jpg') as $filename) {
   if($i++ <= 1000) {
       // Do the display
   } else {
      break;
   }
}

答案 1 :(得分:0)

$i = 0;
$max = 1000;
foreach(array_slice(glob('/res/images/*.jpg'), 0, $max) as $filename) {
   // Some code here
   if($i++ >= $max) break;
}

答案 2 :(得分:0)

试试这个

最短的方式。

$i = 0;
foreach(array_slice(glob('/res/images/*.jpg'), 0, 1000) as $filename) {
   // Some code here
   if($i++ >= 1000) break;
}

我希望它会有所帮助。