在foreach内部循环,直到找到第一个匹配的元素

在foreach内部循环,直到找到第一个匹配的元素

问题描述:

First of all apologies for the vague question title. I couldn't come up with a title that made sense.

I'm looping through image files in a directory using:

$folder = 'frames/*';
foreach(glob($folder) as $file)
{

}

The Requirement

I want to measure each file's size and if it's size is less then 8kb, move to the next file and check it's size and do so until you get the file whose size is greater than 8kb. Right now I'm using

$size = filesize($file);
if($size<8192) // less than 8kb file
{
    // this is where I need to keep moving until I find the first file that is greater than 8kb
   // then perform some actions with that file
}
// continue looping again to find another instance of file less than 8kb

I had a look at next() and current() but couldn't come up with a solution that I was looking for.

So for a result like this:

File 1=> 12kb
File 2=> 15kb
File 3=> 7kb // <-- Found a less than 8kb file, check next one
File 4=> 7kb // <-- Again a less than 8kb, check next one
File 5=> 7kb // <-- Damn! a less than 8kb again..move to next
File 6=> 13kb // <-- Aha! capture this file
File 7=> 12kb
File 8=> 14kb
File 9=> 7kb
File 10=> 7kb
File 11=> 17kb // <-- capture this file again
.
.
and so on

UPDATE

The complete code that I'm using

$folder = 'frames/*';
$prev = false;
foreach(glob($folder) as $file)
{
    $size = filesize($file);    
    if($size<=8192)
    {
       $prev = true;
    }

    if($size=>8192 && $prev == true)
    {
       $prev = false;
       echo $file.'<br />'; // wrong files being printed out    
    }
}

首先为模糊问题标题道歉。 我无法想出一个有意义的标题。 p>

我正在使用以下代码循环目录中的图像文件: p>

   $ folder ='frames / *'; 
foreach(glob($ folder)as $ file)
 {
 
} 
  code>  pre> 
 
 

要求 h3>

我想测量每个文件的大小,如果它的大小小于 8kb code>,请移动到下一个文件并检查它的大小,然后执行此操作,直到获得其文件的大小为止 size大于 8kb code>。 现在我正在使用 p>

  $ size = filesize($ file); 
if($ size&lt; 8192)//小于8kb的文件
 {
 // 这是我需要继续移动的地方,直到我找到大于8kb的第一个文件
 //然后对该文件执行一些操作
} 
 //再次继续循环以查找小于8kb的另一个文件实例 n  code>  pre> 
 
 

我查看了 next() code>和 current() code>,但是无法想出一个 我正在寻找的解决方案。 p>

所以对于这样的结果: p>

  File 1 =&gt;  12kb 
File 2 =&gt;  15kb 
文件3 =&gt;  7kb //&lt;  - 找到一个小于8kb的文件,检查下一个
文件4 =&gt;  7kb //&lt;  - 再次小于8kb,检查下一个
File 5 =&gt;  7kb //&lt;  - 该死的! 再次小于8kb ..移到下一个
文件6 =&gt;  13kb //&lt;  - 啊哈! 捕获此文件
File 7 =&gt;  12kb 
文件8 =&gt;  14kb 
File 9 =&gt;  7kb 
文件10 =&gt;  7kb 
File 11 =&gt;  17kb //&lt;  - 再次捕获此文件
。
。
等等
  code>  pre> 
 
 

UPDATE h3>

我正在使用的完整代码 p>

  $ folder ='frames / *'; 
 $ prev = false; 
foreach(glob($ folder)as $ file)  
 {
 $ size = filesize($ file);  
 if($ size&lt; = 8192)
 {
 $ prev = true; 
} 
 
 if($ size =&gt; 8192&amp;&amp; $ prev == true)
 {
  $ prev = false; 
 echo $ file。'&lt; br /&gt;';  //打印错误的文件
} 
} 
  code>  pre> 
  div>

What you need to do is keep a variable indicating if the previous analyzed file was a small one or a big one and react accordingly.

Something like this :

$folder = 'frames/*';
$prevSmall = false; // use this to check if previous file was small
foreach(glob($folder) as $file)
{
    $size = filesize($file);
    if ($size <= 8192) {
        $prevSmall = true; // remember that this one was small
    }

    // if file is big enough AND previous was a small one we do something
    if($size>8192 && true == $prevSmall)
    {
        $prevSmall = false; // we handle a big one, we reset the variable
        // Do something with this file
    }
}