dnguyen Posted July 10, 2008 Share Posted July 10, 2008 Must be having a brain fart tonight...I want to pass an array into a function and have that function be able to add to it. Like so: function foo($array, $k, $v){ $array[$k] = $v; } $array = array(); foo($array, "key", "value"); echo $array["key"]; //should echo "value" but it does not Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/ Share on other sites More sharing options...
Third_Degree Posted July 10, 2008 Share Posted July 10, 2008 use array_push( ) Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586106 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 You need to pass the array by reference. function foo(&$array, $k, $v) { $array[$k] = $v; } Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586108 Share on other sites More sharing options...
Third_Degree Posted July 10, 2008 Share Posted July 10, 2008 why redefine the array_push function? Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586109 Share on other sites More sharing options...
dnguyen Posted July 10, 2008 Author Share Posted July 10, 2008 You need to pass the array by reference. function foo(&$array, $k, $v) { $array[$k] = $v; } Yeah I tried that, but I get "Fatal error: Cannot pass parameter 3 by reference in..." (in my function, the array is the 3rd parameter). Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586110 Share on other sites More sharing options...
dnguyen Posted July 10, 2008 Author Share Posted July 10, 2008 why redefine the array_push function? [/quote right now, just rewriting a wrapper function that took in those parameters and previously did something else. I don't feel like changing all the function references that were already written into the main program right now. Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586111 Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 Can we see actual code and not your fake code? Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586112 Share on other sites More sharing options...
dnguyen Posted July 10, 2008 Author Share Posted July 10, 2008 ok... function store($txt, $field="", &$array, $table=""){ $txt = trim(iconv('UTF-8', 'ISO-8859-15//TRANSLIT', $txt)); $array[$field] = $txt; echo "<b>$field</b> ". $array[$field]. "<br />"; //this works } // ...main function store( "testvalue", "last_name", $inmate, "names" ); echo "LASTNAME: " .$inmate["last_name"]."<br />"; //nothing outputs (and then I get the, can't pass 3rd parameter by reference error) Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586114 Share on other sites More sharing options...
Third_Degree Posted July 10, 2008 Share Posted July 10, 2008 declare $inmate first as an array OR, pass $inmate by reference as well (&$inmate) Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586115 Share on other sites More sharing options...
dnguyen Posted July 10, 2008 Author Share Posted July 10, 2008 nevermind...the can't pass 3rd parameter by reference error was because I had one function call that was in error. My bad. Link to comment https://forums.phpfreaks.com/topic/114034-solved-passing-an-array-into-the-function-and-altering-it/#findComment-586116 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.