Page 1 of 1

Array of problems with arrays

Posted: Thu Nov 17, 2011 5:17 pm
by toddbuckles
Hi,

I cannot, for the life of me, figure out what is happening. I have the following:

Code: Select all

<?PHP
  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.

  function getFileList($dir, $recurse=false)
  {
    // array to hold return value
    $retval = array();

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      // skip hidden files
      if($entry[0] == ".") continue;
      if(is_dir("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry/"
        );
        if($recurse && is_readable("$dir$entry/")) {
          $retval = array_merge($retval, getFileList("$dir$entry/", true));
        }
      } elseif(is_readable("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry"
        );
      }
    }
    $d->close();

    return $retval;
  }
?>

<?PHP
	$directories = getFileList("apps/plogger-1.0RC1/plog-content/images/varsity-football---2011");
	//echo count($directories);
	
	shuffle($directories);
	
	print $directories[0];
	//print_r($directories[1]);	
	
  // include subdirectories
  $dirlist = getFileList(($directories[1]), true);
  //$dirlist = getFileList("apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/uhs-spring-2011-game-vs.-poincianna", true);
?>
About ten lines up (directories = getFileList...) I am trying to get a list of the folders in that folder. when I print_r($directories), I get the following output:

Array ( [0] => Array ( [name] => apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/guestbook/ ) [1] => Array ( [name] => apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/2011-gateway-game/ ) [2] => Array ( [name] => apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/varsity-football---2011-kickoff-classic-vs.-ocoee/ ) [3] => Array ( [name] => apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/2011-lake-mary-game/ ) [4] => Array ( [name] => apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/uhs-spring-2011-game-vs.-poincianna/ ) [5] => Array ( [name] => apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/index.php ) [6] => Array ( [name] => apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/2011-hagerty-game/ ) [7] => Array ( [name] => apps/plogger-1.0RC1/plog-content/images/varsity-football---2011/2011-deltona-game/ ) )

I want to pull one random path from this array. When I

Code: Select all

foreach($directories as $v){
echo $v."<br />";
}
resultant output is the word Array about ten times. I have tried echo array_slice($directories,1,1), and echo $directories[0], echo $directories[1] and so forth, each time the word Array, not the contents of the 1st or 2nd element.

I am simply trying to select photos from a random folder on each page load. grrr. I hate my job, anyone hiring beer taste testers???

Thanks in advance,

TB

Re: Array of problems with arrays

Posted: Thu Nov 17, 2011 5:39 pm
by Celauran

Code: Select all

foreach ($directories as $directory)
{
    echo $directory['name'] . "<br />";
}

Re: Array of problems with arrays

Posted: Thu Nov 17, 2011 11:51 pm
by toddbuckles
Great help. In order to select one, I modified a bit like this.

Code: Select all

$directories = getFileList("apps/plogger-1.0RC1/plog-content/images/varsity-football---2011");
	shuffle($directories);
  // include subdirectories
  $dirlist = getFileList(($directories[1]['name']), true);
In any case, as you pointed out it was because it was a 3 dim array, and needed the key named.

Thanks a ton for you help.

TB