TGWSE_GY Posted October 5, 2009 Share Posted October 5, 2009 I am wanting to create an array on the fly, when two if conditions are true then I can add the file name to the array. As it stands right now it adds nothing to the array and I think it may have to do with array_push(). Here is the code, any thoughts? <?php //Get config file include('content/conf/config.site.php'); //Get directory $directory = $abs_site_root . $dir_content; $dir = dir("$directory"); $count = 1; $filearray = array(0 => "seed"); //List files in images directory while (($file = $dir->read()) !== false) { if($file != "."){ if($file != ".."){ $search = ".php"; $replace = ""; $count ++; $file = str_replace($search, $replace, $file); array_push($filearray, $count, $file); } } } $dir->close(); ?> Thanks guys!!! Link to comment https://forums.phpfreaks.com/topic/176517-php-arrays-adding-elements-when-conditions-are-met-in-a-loop/ Share on other sites More sharing options...
RussellReal Posted October 5, 2009 Share Posted October 5, 2009 $filearray[] = "Value for the new array element"; Link to comment https://forums.phpfreaks.com/topic/176517-php-arrays-adding-elements-when-conditions-are-met-in-a-loop/#findComment-930500 Share on other sites More sharing options...
TGWSE_GY Posted October 5, 2009 Author Share Posted October 5, 2009 Thanks RussellReal works like a charm I also wanted to post my working code in hopes that it helps someone else someday <?php //Get config file include('content/conf/config.site.php'); //Get directory $directory = $abs_site_root . $dir_content; $dir = dir("$directory"); $filearray = array(0 => "seed"); //List files in images directory while (($file = $dir->read()) !== false) { if($file != "."){ if($file != ".."){ $search = ".php"; $replace = ""; $file = str_replace($search, $replace, $file); $filearray[] = $file; } } } $dir->close(); ?> Link to comment https://forums.phpfreaks.com/topic/176517-php-arrays-adding-elements-when-conditions-are-met-in-a-loop/#findComment-931052 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.