For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2006 > returning and array from a function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author returning and array from a function
Chris

2006-05-25, 3:58 am

I am clearly missing something simple here....

I have a function (fileTree) that recursively reads all the files in a
directory passed to it, and returns an array of the files, I have included
the code for this function below.

When I call this function, the bit of debug code at the end will correctly
print the array of files I am expecting, and then returns.

However, what is returned form the function isnt the array, is a scalar that
just contains the value of the last element in the
array

I am calling the function like this :

$BrokenArray = array();
$BrokenArray = $this->fileTree($this->ProjectRoot,".c");

foreach (($this->fileTree($this->ProjectRoot,".c")) as $wibble)
{
print "Broken Array : $wibble <br>\n";
}


So it seems that the array in the function contains the right data, and by
doing "$BrokenArray = array();" i suspect I am making sure the varible it is
being assinged to isnt a scalar, so the only thing I can think of of is that
I need to do something fancy with the "return" statement so it knows it's a
array, but so far I have not not found anything that would help...

Cheers

Chris





function fileTree($directory,$ext)
{

$dir_array = array();
$file_array = array();

// what is the length of the extension
$ExtLen = strlen($ext);

if ($handle = opendir($directory))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
// this is a directory, so recurse down it
if (is_dir($directory. "/" . $file))
{
$dir_array = array_merge($dir_array, $this->fileTree($directory.
"/" . $file,$ext));
$file = $directory . "/" . $file;
$dir_array[] = preg_replace("/\/\//si", "/", $file);
}

// this is a file, test it, and add it to list if need be
else
{

$file = $directory . "/" . $file;

// overall length of the $file string
$FileLen = strlen ($file);

// now match the last N chars with the extension
if ((substr ($file,($FileLen-$ExtLen),($FileLen))) == $ext)
{
$this->fileArray = array_merge($this->fileArray,$file);
$file_array = array_merge($file_array,$file);
}

}
}
}

closedir($handle);
}

// print out the array of files, to prove it is getting all the files
foreach ($file_array as $wibble)
{
print "Wibble : $wibble <br>\n";
}

return ($file_array);
}


Janwillem Borleffs

2006-05-25, 6:58 pm

Chris wrote:
> So it seems that the array in the function contains the right data,
> and by doing "$BrokenArray = array();" i suspect I am making sure
> the varible it is being assinged to isnt a scalar, so the only thing
> I can think of of is that I need to do something fancy with the
> "return" statement so it knows it's a array, but so far I have not
> not found anything that would help...


When you increase your error_reporting level, you will notice that you are
passing array_merge a scalar as the second argument, while it expects arrays
only:

$file_array = array_merge($file_array,$file);

To fix this, don't use array_merge, but simply:

$file_array[] = $file;


JW


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com