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");

.

.

.

  }

 

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.