Jump to content

Recommended Posts

I have an array.

One of the items in the array is also an array.

 

I'm passing the array into a function.

The functions changes the contents of the array inside the array.

 

Problem: the changes only are visible inside the function.

Outside: there's no trace of the variables set inside the array's array.

 

I've tried adding the array's array into the function: but it doesn't like that!

 

My function decleration is something like this:

 

function myFunction($allVariables, $allVariables['subArray'])
{
//
}

 

What am I doing wrong...?

 

Thanks.

 

 

OM

Link to comment
https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/
Share on other sites

guys: thanks for the lighting replies!

maybe i'm wrong, but i thought one of the advantages of using an array was that you could access items inside of them and change them?

 

previously, the same function used to only work with a single dimensional array.

this was accessed by calling global.

this worked fine.

but then i created an array and made it an array inside of a global array.

i then started passing in just the global array itself inside the function.

i was then referencing the array inside by myArray['var1']['subVar1'] etc.

 

i can pass a new variable back out - but i was sure that if u passed in a an array that u could manipulte its inside and that those changes were still valid outside?

 

let me know what u think.

 

thanks.

hmm... yes... i found that out by experimenting.

what threw me offline a little was when i used global: it works fine when i use this.

 

but, i'll do as u guys suggest and pass back a variable from the function.

actually... for that part... if i pass back an array, do all the values changed in the array stay?

what if my array was really huge?

does that mean a huge amount of data could potentially have to be passed through?

and therefore being inefficient - and therefore a good reason to use global?

 

let me know what you think.

thanks.

 

 

 

you can pass the array by reference instead of passing a copy of the aray

<?php
function double_it (&$array)                                     // function receives array by reference
{
    foreach ($array as $k=>$v) $array[$k] = $v * 2;
}

// call the function, passing the array

$ar = array (1,2,3);
double_it ($ar);

echo '<pre>', print_r($ar, true), '</pre>';
?>

gives -->

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)

It's certainly a good idea to keep the use of globals to an absolute minimum, preferably zero. Better to pass them as args to functions. You only need to pass them by reference if you want to change their value in the function.

 

I also tend to pass arrays by ref, even when not updating their content, as it reduces memory usage. If you don't pass them by ref a copy is passed to the function, and with large arrays that's all extra memory and time.

Here's a quick question re: pass-by-reference...

 

In PHP 5, object assignment is automatically done by reference, correct?  So:

$obj1 = new Object();
$obj2 = $obj1;
$obj2->add(5);

//obj1 added 5 also, because they both reference the same object, right?

 

Does the same work for objects passed as arguments to another object's method?

//front controller code

$command = CommandResolver::getCommand($request); // <-- is $request a reference to the object, or a copy of it?

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.