473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

returning an array from a function

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);
}
May 25 '06 #1
3 1699
tim

Chris wrote:

Hi

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. 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);
}
In the above code, the files found in the subdirectories aren't being
added to $file_array.

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.
Try print "In new dir<br>" and just before the foreach loop to see what
I mean, its finding each file but its not passing what it found to the
calling function.
foreach ($file_array as $wibble)
{
print "Wibble : $wibble <br>\n";
}

return ($file_array);
}


Hope that helps,

Tim

May 25 '06 #2
"Chris" <Ch***@dustbubble.nospam.com> wrote:

I am clearly missing something simple here....
...
I am calling the function like this :

$BrokenArray = array();
$BrokenArray = $this->fileTree($this->ProjectRoot,".c");
Do you understand that the first statement is useless? This creates an
empty array, assigns it to $BrokenArray, and then promptly deletes it, to
be replaced by whatever fileTree returns.
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,


No. You can't change what fileTree returns. If it returns an array,
$BrokenArray will be an array. If it returns a scalar, $BrokenArray will
be a scalar. $this->fileTree has absolutely no clue that $BrokenArray
exists. If fileTree is supposed to return an array, then it must construct
an array and return it.

The other Tim gave you the rest of the hints you need to make this work.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
May 26 '06 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chris wrote:
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";
}


Debug that by using var_dump or print_r :

$BrokenArray = array();
$BrokenArray = $this->fileTree($this->ProjectRoot,".c");
var_dump($BrokenArray);
By the way, remember that PHP is a dinammically-typed language: $BrokenArray
is an array at first, but if you assign it a value of other type (an
integer, for example), it becomes that type.

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

There may still be two superpowers on the planet: the United States and
world public opinion.
-- Patrick Tyler
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFEdqDC3jcQ2mg3Pc8RAqiuAJ9lALLpM6imxVP5J9Zvn6 nibiK3BgCcDlyI
QilzLASAx7ZZrIoe7S5mkB4=
=bi1H
-----END PGP SIGNATURE-----
May 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.