OM2 Posted July 27, 2008 Share Posted July 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/ Share on other sites More sharing options...
unkwntech Posted July 27, 2008 Share Posted July 27, 2008 It's the scope. Variables inside the function are not accessible out side of it. add this to the end of your function: return $varName; This will make it available outside the function Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601022 Share on other sites More sharing options...
Nhoj Posted July 27, 2008 Share Posted July 27, 2008 Any variables changed or declared inside a function stay inside the function. If you want to save the changes to $allVariales you would need to add: return $allVariables; at the end of the function before the last closing brace Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601023 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601028 Share on other sites More sharing options...
Nhoj Posted July 27, 2008 Share Posted July 27, 2008 Unfortunately anything you pass into a function stay's in the function unless you return it at the end. Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601033 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601038 Share on other sites More sharing options...
Barand Posted July 27, 2008 Share Posted July 27, 2008 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601126 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 hey thanks for that! i was just about to give up and use global. that works nicely. Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601132 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 actually... i just had a thought: is it a good idea to pass all gloabls varaibles by reference? or is that just dangerous to do? Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601136 Share on other sites More sharing options...
Barand Posted July 27, 2008 Share Posted July 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601140 Share on other sites More sharing options...
KevinM1 Posted July 27, 2008 Share Posted July 27, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601145 Share on other sites More sharing options...
Barand Posted July 27, 2008 Share Posted July 27, 2008 yes, passed by ref in v5. You have specifically to clone if you want a copy Quote Link to comment https://forums.phpfreaks.com/topic/116880-problems-with-multidimensional-arrays/#findComment-601154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.