consultant1027 Posted March 22, 2007 Share Posted March 22, 2007 Anyone know how to push values to an array that was passed to a function by reference. This doesn't work: function something(&$somearray) { array_push($somearray,"new value"); } Link to comment https://forums.phpfreaks.com/topic/43897-push-to-array-passed-to-function-by-reference/ Share on other sites More sharing options...
consultant1027 Posted March 22, 2007 Author Share Posted March 22, 2007 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 https://forums.phpfreaks.com/topic/43897-push-to-array-passed-to-function-by-reference/#findComment-213089 Share on other sites More sharing options...
consultant1027 Posted March 22, 2007 Author Share Posted March 22, 2007 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 https://forums.phpfreaks.com/topic/43897-push-to-array-passed-to-function-by-reference/#findComment-213108 Share on other sites More sharing options...
rtconner Posted March 22, 2007 Share Posted March 22, 2007 does $somearray[] = "new value"; work? Link to comment https://forums.phpfreaks.com/topic/43897-push-to-array-passed-to-function-by-reference/#findComment-213115 Share on other sites More sharing options...
consultant1027 Posted March 22, 2007 Author Share Posted March 22, 2007 I got it to work. One of those things where something is slightly mispelled an you don't notice it. Arghh! Link to comment https://forums.phpfreaks.com/topic/43897-push-to-array-passed-to-function-by-reference/#findComment-213120 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.