foreach(array as $var except for specific value)
Moderator: General Moderators
foreach(array as $var except for specific value)
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!
- 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)
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.
- 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)
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.
Re: foreach(array as $var except for specific value)
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)
- 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)
Well glob() is more flexible in that you "can" match a pattern, but if all you want is all files and dirs, then: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)
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.
Re: foreach(array as $var except for specific value)
OK I'm trying to implement your suggestion but can't seem to get glob to work. I tried replacing the
with
But I'm now not getting anything returned. Is my syntax wrong? Thanks
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
- 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)
Pay attention to double and single quotes: http://www.php.net/manual/en/language.types.string.phpdb579 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
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.
Re: foreach(array as $var except for specific value)
Thanks a lot for the help. Think I've worked it out now!