1

Okay, I really don't have any idea on how to explain this.

I have a session array:

$_SESSION['users']['currentuser']['username'] = 'stijn';

this array is build dynamically. So, i need to remove some values of this array also dynamically.

for this, I have a function:

function removeSessionValue($keys) {
                $keys = explode(':', $keys);
                var_dump(array_keys($_SESSION));
                $tempArray = array();
                $reference = &$tempArray;
                foreach ($keys as $key) {
                    $reference[$key] = array();
                    $reference = &$reference[$key];
                }
                $multiArray = $tempArray;
            }

function call= removeSessionValues('users:currentuser:username');

So now I have the originarray (session) and the array to check if the session exists (built by the function).

Is there any way, on how I can unset the $_SESSION['user']['currentuser']['username'] ?

Important note, we don't know what values will be passed in the function, as also we don't know what sessions exists, as everything is ultradynamic ...

1 Answer 1

1

A simple way is use eval():

function deepUnset(&$array, $keys)
{
    $cmd = 'unset($array["'.implode($keys, '"]["').'"]);';
    eval($cmd);
}

// Example:
$_session = array(
    'users' => array(
        'currentUser' => array(
            'username' => 'stijn',
        ),      

        'otherUser' => array(
            'username' => 'james',
        ),
    )
);

echo '<pre>';
print_r($_session);
deepUnset($_session, array('users', 'currentUser', 'username'));
print_r($_session);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.