Jump to content

PHP Arrays - Adding Elements when conditions are met in a loop.


TGWSE_GY

Recommended Posts

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!!!  8)

Thanks RussellReal works like a charm :)

 

I also wanted to post my working code in hopes that it helps someone else someday  :hail_freaks:

 

<?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();

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.