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)

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.