[SOLVED] calling functions with arrays in the parameter list
05-08-08, 07:26 PM
Newbie Coder
Join Date: Mar 2008
Location: Canada, midwest
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] calling functions with arrays in the parameter list
Trouble passing arrays by reference (or to a function at all). Here's my code (simplified):
only returns:
array(0){}
Last edited by Nico; 05-09-08 at 03:29 AM .
Reason: [php] wrappers.
05-08-08, 07:58 PM
Code Master
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
without knowing if the condition or something are actually true, its hard to say. Chances are, they are not resolving as true
05-09-08, 11:13 AM
Newbie Coder
Join Date: Mar 2008
Location: Canada, midwest
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Jay,
Sorry for the brevity of the script. I can echo the array element in build_array fn, but var_dump in the main doesn't produce anything useful.
I should be able to access any new elements on its return if I pass an array as part of the fn's parameter list by reference (&), right?
Fred
05-09-08, 11:20 AM
Newbie Coder
Join Date: Mar 2008
Location: Canada, midwest
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
I added:
global $counter
to the build_array fn because echo-ing $counter revealed $counter was sometimes empty. This seems counter-intuitive to me. If its part of the parameter list and passed by reference, why should it have to be declared globally too?
05-09-08, 11:23 AM
Newbie Coder
Join Date: Mar 2008
Location: Canada, midwest
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
adding
global $my_array1;
global $my_array2;
has seemd to clear up my problem. If there's any explaination (or link) about passing by ref and the need globals as well I would appreciate it.
thanks.
05-09-08, 11:28 AM
Code Master
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
You should not need to use global. Here's an example that works without it
05-09-08, 11:35 AM
Newbie Coder
Join Date: Mar 2008
Location: Canada, midwest
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Jay,
Here is the code in full, i'm sorry i didn't mention that there is a recursive call in the fn.
hope this helps, sorry for not mentioning it earlier.
fred
Last edited by Nico; 05-09-08 at 11:53 AM .
Reason: Please use [php] wrappers when posting PHP code.
05-09-08, 11:49 AM
Code Master
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:
<?php function MakeTree ( $f , & $file_array , & $time_array , & $counter ) { if( is_dir ( $f )) { foreach( glob ( $f . '/*' ) as $sf ) { $sf_base = basename ( $sf ); if( is_dir ( $sf )) { MakeTree ( $sf_base , $file_array , $time_array , $counter ); } } } else { $file_array [ $counter ] = $f ; $mod_time = date ( "YmdB" , @ filemtime ( $f )); //year month day swatch $time_array [ $counter ] = $mod_time ; echo "counter ( $counter )" ; echo $file_array [ $counter ], $time_array [ $counter ], "<br>" ; ++ $counter ; } } $root_dir = "." ; $file_array = array(); $time_array = array(); $counter = 0 ; if(! $dp = opendir ( $root_dir )) { ErrorMessage ( "Can't open $root_dir " ); } else { while( false !== ( $file = readdir ( $dp ))) { if( $file === '.' ) { MakeTree ( $file , $file_array , $time_array , $counter ); } } } var_dump ( $file_array ); ?>
Try that. You had a number of errors in the script. one was the structure of the if's and else ifs, another was the MakeTree in the recursive function had only one parameter. Also, can you use the [PHP] [/PHP] wrappers when posting your code please
05-10-08, 01:37 PM
Newbie Coder
Join Date: Mar 2008
Location: Canada, midwest
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Jay! I guess I was a few parameters short a load ;-)
I will also use the wrappers in future quesitons.
Fred
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off