Jump to content

Unset variable/array which has been passed to function


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

    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.



Edited by matthewtbaker
Link to comment
Share on other sites

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?


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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