how to restrict duplication of array in php?
i am searching file using following code but in output it shows result 5 times. this is the code. if something wrong in the code then plz tell.
$root1 = $_SERVER['DOCUMENT_ROOT'];
$directory =
$root1.'/Place4Info/Data/Admin_data/';
$it = new
RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
while($it->valid())
{
if(!$it->isDot())
{
$files =glob($directory.$it->getSubPath().'/*.jpg');
print_r($files);
}
$it->next();
}
& the output is :-
Array ( [0] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports19.jpg [1] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports21.jpg [2] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports22.jpg [3] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports3.jpg ) Array ( [0] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports19.jpg [1] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports21.jpg [2] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports22.jpg [3] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports3.jpg ) Array ( [0] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports19.jpg [1] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports21.jpg [2] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports22.jpg [3] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports3.jpg ) Array ( [0] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports19.jpg [1] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports21.jpg [2] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports22.jpg [3] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports3.jpg ) Array ( [0] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports开发者_开发百科19.jpg [1] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports21.jpg [2] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports22.jpg [3] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/admin/sports3.jpg ) Array ( ) Array ( [0] => C:/Program Files/EasyPHP-5.3.2i/www/Place4Info/Data/Admin_data/pratikg88/matromonial24.jpg )
you are calling print_r
every time a new file is added. move it after your while loop
$files = array();
while($it->valid()) {
if(!$it->isDot()) {
$files = array_merge($files, glob($directory.$it->getSubPath().'/*.jpg'));
}
$it->next();
}
print_r($files);
your array is fine, you are simply printing it too often
edit
i assume you want to retrieve all jpg files from your directories, there's a good example in on of the comments on RecursiveDirectoryIterator manual page on php.net:
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('path/to/project/'));
$Regex = new RegexIterator($it, '/^.+\.jpg$/i', RecursiveRegexIterator::GET_MATCH);
quote:
$Regex will contain a single index array for each PHP file.
精彩评论