Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] calling functions with arrays in the parameter list


[SOLVED] calling functions with arrays in the parameter list

Reply
  #1 (permalink)  
Old 05-08-08, 07:26 PM
FredSmith FredSmith is offline
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):
PHP Code:

function build_array(&$my_array, &$my_array2, &$counter) {
    while( 
something true ) {
        if ( 
condition true ) {
            
$my_array1[$counter]= "some data";
            
$my_array2[$counter]= "some data";
            ++
$counter;
        }
    }
}

$my_array1 = array();
$my_array2 = array();
$counter 0;
build_array ($my_array1$my_array2$counter);
var_dump ($my_array1); 
only returns:
array(0){}

Last edited by Nico; 05-09-08 at 03:29 AM. Reason: [php] wrappers.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-08-08, 07:58 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
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
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-09-08, 11:13 AM
FredSmith FredSmith is offline
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-09-08, 11:20 AM
FredSmith FredSmith is offline
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-09-08, 11:23 AM
FredSmith FredSmith is offline
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 05-09-08, 11:28 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
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
PHP Code:

function add_to_array(&$array,&$counter)
{
    while(
$counter 5)
    {
    
$array[$counter]='VALUE ADDED';
    ++
$counter;
    }
}
$f = array();
$counter 0;
add_to_array($f,$counter);
echo 
'COUNTER: ',$counter,'<br />';
echo 
'<pre>'.print_r($f,true).'</pre>'
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 05-09-08, 11:35 AM
FredSmith FredSmith is offline
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.
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);
      } }
      else {
          
$file_array[$counter] = $sf;
          
$mod_time date("YmdB", @filemtime($sf));   //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);
?>
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 05-09-08, 11:49 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
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
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 05-10-08, 01:37 PM
FredSmith FredSmith is offline
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help calling and using a functions in visual c++ 6 method C/C++ 0 07-08-06 06:22 PM
arrays in functions Slypher PHP 3 01-30-05 06:36 AM
Calling MYSQL table & list entrys in dropdown menu doublee313 PHP 1 09-25-04 05:29 AM


All times are GMT -5. The time now is 01:33 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.