Jump to content

[SOLVED] Passing an array into the function, and altering it


dnguyen

Recommended Posts

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

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).

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.

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)

 

 

Archived

This topic is now archived and is closed to further replies.

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