foreach(array as $var except for specific value)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

foreach(array as $var except for specific value)

Post by db579 »

I'm trying to use a foreach loop to echo each value stored in an array as variable but as the array is created using the scandir function there are also an extra '.' and '..' value entered (not entirely sure where these come from) which I don't want stored as variable. Have tried reading the foreach manual but can't find any syntax for excluding particular values from the loop. How do I do this? Thanks very much!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach(array as $var except for specific value)

Post by AbraCadaver »

I would use glob() instead of scandir(), but to answer your question:

Code: Select all

foreach($array as $var) {
   if($var != '.' && $var != '..') {
      echo $var;
   }
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach(array as $var except for specific value)

Post by AbraCadaver »

And the . is the current directory and .. is the parent directory.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

Re: foreach(array as $var except for specific value)

Post by db579 »

This works perfectly, thanks very much!! Out of interest though why the glob function rather than scandir? (I'm using scandir to return the names of directories so I won't have an extension or anything to use as the pattern parameter)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach(array as $var except for specific value)

Post by AbraCadaver »

db579 wrote:This works perfectly, thanks very much!! Out of interest though why the glob function rather than scandir? (I'm using scandir to return the names of directories so I won't have an extension or anything to use as the pattern parameter)
Well glob() is more flexible in that you "can" match a pattern, but if all you want is all files and dirs, then:

Code: Select all

glob('/path/*');

//for only dirs
glob('/path/*', GLOB_ONLYDIR);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

Re: foreach(array as $var except for specific value)

Post by db579 »

OK I'm trying to implement your suggestion but can't seem to get glob to work. I tried replacing the

Code: Select all

$array = scandir($path);


with

Code: Select all

$array =  glob('$path*', GLOB_ONLYDIR);


But I'm now not getting anything returned. Is my syntax wrong? Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: foreach(array as $var except for specific value)

Post by AbraCadaver »

db579 wrote:OK I'm trying to implement your suggestion but can't seem to get glob to work. I tried replacing the

Code: Select all

$array = scandir($path);


with

Code: Select all

$array =  glob('$path*', GLOB_ONLYDIR);


But I'm now not getting anything returned. Is my syntax wrong? Thanks
Pay attention to double and single quotes: http://www.php.net/manual/en/language.types.string.php
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

Re: foreach(array as $var except for specific value)

Post by db579 »

Thanks a lot for the help. Think I've worked it out now!
Post Reply