Unset two-dimensional associative session array

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Working with a two-dimensional associative array:

<?php
//array sessions are set as follows
$_SESSION['test']['car']['make']="ford";
$_SESSION['test']['car']['colour']="red";

$_SESSION['test']['bike']['make']="ducati";
$_SESSION['test']['bike']['colour']="yellow";

//and unset as follows
foreach($remove as $rem_key => $rem_value){
if (
array_key_exists($rem_key, $_SESSION['test'])){
unset(
$_SESSION['test'][$rem_key]);
}
}
?>

I have a form with checkboxes that allows removal of the items listed from the array. The form checkbox input name is remove[$session_key]
$remove is an array with keys that match the keys of the session for any selected (checked)
$remove values are all simply "delete" (not really needed although I do check before the foreach if = "delete")

The arrays set fine, printed all look ok and as expected. The above with both the car and bike:
Array ( [car] => Array ( [make] => ford [colour] => red ) [bike] => Array ( [make] => ducati [colour] => yellow ) )

I can put one array in the session and use the form to select to delete the item, and it works fine.
But when there are two or more items in the session, as with the above (car & bike) it seems to mess up the array.
Is this something to do with the indexes when one is removed within a foreach?

Or something else I'm missing?
Cheers

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

Are you using cookies (default handler) for the session vars?

Maybe inspecting the cookie at each step will provide a clue.

Isn't it actually a 3-dimensional array? ['test']['car']['make']

So I would think your removal code may work if you add the additional level - because for one array, it is essentially two-dimensional, the first two keys are always the same, only the third one changes...

$_SESSION['test']['car']['make']="ford";
$_SESSION['test']['car']['colour']="red";

greg's picture

He has: 1,581 posts

Joined: Nov 2005

decibel.places wrote:
Are you using cookies (default handler) for the session vars?
Maybe inspecting the cookie at each step will provide a clue.
yeah as I showed above printing the array, it's storing it perfectly, there's just something wrong with the removal. I thought perhaps something to do with the unsetting within a loop and change of the session/array index within the loop, although it's not the array the loop is using that I change...

decibel.places wrote:
So I would think your removal code may work if you add the additional level - because for one array, it is essentially two-dimensional, the first two keys are always the same, only the third one changes...

$_SESSION['test']['car']['make']="ford";
$_SESSION['test']['car']['colour']="red";

['test'] is the session name, albeit the session is an array, ['car'] is a key in the session array and the value for ['car'] is another array where ['make'] is one key and ford is the value, ['colour'] is another key and red is the value.

$rem_key would be "car" or "bike" and unset($_SESSION['test'][$rem_key]); should remove that full array from the session

They have: 121 posts

Joined: Dec 2008

What is messed up about the array once you've done an unset on it?
Are you missing data? Is there too much data?

Cheers,
Shaggy.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I'm just getting vibes that having the same keys (make,colour) in two different arrays is somehow related to your problem...

Have you tested it out with all unique keys?

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Duplicate index refs (key names) in this instance are fine as they are different/separate arrays.
Otherwise PHP would hit an error creating them, not deleting them.

As you both mentioned, echoing out at every stage is the way to find errors, and I had done this (always do) I just didn't do it on another block of code that was also accessing the session.
(truth be known I thought I'd commented it out while testing this block Doh!)

So as often is the case, it was that other piece of code that was causing the issue to the session, not the above delete loop.
And in the end was simply my numbtiness for not having commented out some untested code.

So all is well now, cheers.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.