简体   繁体   English

通过遍历数组php查找字符串

[英]Finding a string by looping through an array php

I have this array $filelist 我有这个数组$ filelist

Array ( [0] => . [1] => .. [2] => .DS_Store [3] => 11-96.eml [4] => 11-97.eml ) 

Which is a list of all files in a particular directory... Sometimes there is no .DS_Store file, sometimes there is, sometimes there are 2 eml files, sometimes there are as many as 6. I'm trying to loop through and find the first array position a .eml file exists so that I can work out how many file are eml and where to start referencing the array.. I have tried... 这是特定目录中所有文件的列表...有时没有.DS_Store文件,有时有,有时有2个eml文件,有时有多达6个。我试图遍历并查找存在一个.eml文件的第一个数组位置,以便我可以计算出eml文件的数量以及从何处开始引用该数组..我已经尝试过...

 function firstFileInList($filelist) {
        $x = 0;
        foreach ($filelist as $value) {
          if(strpos($filelist, ".eml") == false) {
              $x = $x + 1;   
             //break;
            }

          }

          return $x;
      }

This returns 5, if I include the break it returns 1 and I was expecting to get 4. 这将返回5,如果我包含break,则它将返回1,并且我希望得到4。

Please could somebody point me in the right direction or even if there is a completely better way to do this then I would be more than grateful to see that... 请有人指出正确的方向,或者即使有完全更好的方法来做到这一点,那么我也将不胜感激……

Thanks Paul, 谢谢保罗,

break exists every PHP loop directly when it is called, even if there are other elements. 即使在调用PHP循环时, break也会直接存在,即使有其他元素也是如此。 Use continue to get to the next element. 使用continue来进入下一个元素。

Based on your question 根据您的问题

I'm trying to loop through and find the first array position a .eml file exists 我试图遍历并找到存在.eml文件的第一个数组位置

function firstFileInList($filelist) {
    foreach($filelist as $k => $v)
      if(strpos($v, ".eml") !== false)
        return $k;

    return false;
}

The best way to grab a key from an array in a foreach is to define the key before it starts. 从foreach中的数组中获取键的最佳方法是在键开始之前对其进行定义。 Try updating your code with this: 尝试使用以下方法更新代码:

function firstFileInList($filelist) {
    $x = false;
    foreach ($filelist as $key => $value) {
        if(strpos($value, ".eml") == false) {
            $x = $key;   
            break;
        }
    }
    return $x;
}

What this does is set $x to the actual key instead of a number that you increment like you would in a for() loop. 这样做是将$ x设置为实际键,而不是像for()循环中那样递增的数字。 $key will always be the array key, so in this example 0, 1, 2, 3, 4. $ key将始终是数组键,因此在此示例中为0、1、2、3、4。

 function firstFileInList($filelist) {
 $key=false;
    foreach ($filelist as $key=>$value) {
      if(strpos($value, ".eml") !==false){ 
         break;
        }
      }
      return $key; 
  }

In case there is no match you get false. 如果没有匹配项,您将得到假。

The problem lies here: 问题出在这里:

 foreach ($filelist as $value) {
     if(strpos($filelist, ".eml") == false) {

Note that, for the foreach loop as you have written, it takes each element of the $filelist array, and puts it into the $value variable. 请注意,对于您所编写的foreach循环,它将获取$filelist数组的每个元素,并将其放入$value变量中。 Maybe you don't have warnings turned on in PHP, but when I tried your code, I got the following: 也许您没有在PHP中打开警告,但是当我尝试您的代码时,得到了以下信息:

Warning: strpos() expects parameter 1 to be string, array given in test/a.php on line 6

What you want is 你想要的是

foreach ($filelist as $value) {
    if(strpos($value, ".eml") == false) {
        $x = $x + 1;
    }           
}

Note $value in the second line. 注意第二行中的$value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM