Finding a string by looping through an array php

by Paul Leppard   Last Updated May 18, 2015 03:11 AM

I have this array $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...

 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.

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,



Answers 4


The best way to grab a key from an array in a foreach is to define the key before it starts. 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. $key will always be the array key, so in this example 0, 1, 2, 3, 4.

Chris Evans
Chris Evans
May 17, 2015 15:32 PM

break exists every PHP loop directly when it is called, even if there are other elements. Use continue to get to the next element.

Based on your question

I'm trying to loop through and find the first array position a .eml file exists

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

    return false;
}
Richard
Richard
May 17, 2015 15:32 PM

 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.

tanja
tanja
May 17, 2015 15:39 PM

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. Maybe you don't have warnings turned on in PHP, but when I tried your code, I got the following:

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.

Kai Yao
Kai Yao
May 17, 2015 15:41 PM

Related Questions


Updated July 17, 2018 18:26 PM

Updated June 28, 2017 07:26 AM

Updated January 23, 2019 04:26 AM

Updated July 27, 2017 22:26 PM

Updated January 26, 2019 14:26 PM