Jump to content

Push to Array passed to Function by Reference?


consultant1027

Recommended Posts

Maybe I should elaborate:

 

I have a function that assigns values to three arrays.  It seems the best way to do this is pass them as arrays.  So I'm calling the function like:

 

setarrayvalues(&$array1,&$array2,&$array3);

 

Then a function that sets the values:

 

function setarrayvalues(&$somearray1,&$somearray2,&$somearray3)

  {

  array_push($somearray1,"some value");

.

.

.

  }

 

I found initially my problem is that I wasn't initialliazing the array anywhere (still living in the PERL world).  But I have to initialize the arrays in the function it doesn't work if I initialize them in the main program before I call the function!  Doesn't make sense to me.  At any rate the array is empty when it comes back from the function!

 

function setarrayvalues(&$somearray1,&$somearray2,&$somearray3)

  {

  $somearray1 = array();

  $somearray2 = array();

  $somearray3 = array();

  array_push($somearray1,"some value");

.

.

.

  }

 

Heck, I can't even get something like this to work.  Is there a problem in PHP 4.4.x with passing variables to functions as references?

 

function change(&$somevar)

{

$somevar = 20;

}

 

$test = 10;

change($test);

 

 

$somevar is empty inside the function and after exectuing it $test is still 10!

 

Help!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.