Jump to content

Unset variable/array which has been passed to function


matthewtbaker

Recommended Posts

Just a thought...

 

Is it possible to unset a global variable/array from within a function?  So basically I want to pass an array to a function to convert to a string then destroy the original array.  I'd like to keep it all contained within the function to reduce any additional clear-up scripting required.

 

Here's my example code below:

$GLOBALARRAY = array("keyname1" => "value1", "keyname2" => "value2");

$newstring = convertArrayToString($GLOBALARRAY, true);



function convertArrayToString($array, $unset = false) {

    $string = '';

    //Loop for each array value
    foreach ($array as $key => $value) {
        $string = $string . $key . '=' . $value ';';
    }

    if ($unset === true) {
        unset($GLOBALARRAY_WHICH_WAS_PASSED);
    }

    return $string;
} 

My guess is that I need to either get the name of the passed array or pass by reference?

 

Thanks for your thoughts.

 

Matt

  On 5/13/2013 at 5:19 PM, Dathremar said:

If you want you in-function changes to have effect on a global variable, like You said pass that variable by reference inside the function and change it.

 

But changing an array to null for instance  isn't the same as unset, is it?

 

$array=null;

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.