Reputation: 87
I am having a hell of a time with this. I have a multidimensional array that I am storing in a session.
$d1 = array(1,2,3,4);
$d2 = array(1,2,3,4,5,6);
$d3 = array(1,2,3,4,5,6,7,8);
$d4 = array(1,2,3,4,5);
$_SESSION['array1'] = array($d1,$d2,$d3,$d4);
what I want to do is remove the $d2 array from the session array1
however when I do something like this
unset($_SESSION['array1'][1]);
you would think that $_SESSION['array1'] would then = array($d1,$d3,$d4);
however what that does is actually unset the whole session variable.
Then if I try something like
foreach ($_SESSION['array1'] as $k => $v) {
echo "The Key is $k: The Value is $v";
}
however that gives me an error
Invalid argument supplied for foreach()
The only conclusion that I can come to is that the session variable is being completely unset, not that just the specific key is being removed from the array.
is there any way that i can unset a specific value contained within an array that is part of a session variable?
Upvotes: 3
Views: 157
Reputation: 13534
Use array_splice as the following code shows:
$_SESSION['array1'] = array_splice($_SESSION['array1'],1,0);
Upvotes: 1
Reputation: 53129
Code you present works as expected:
header("Content-Type: text/plain");
session_start();
$d1 = array(1,2,3,4);
$d2 = array(1,2,3,4,5,6);
$d3 = array(1,2,3,4,5,6,7,8);
$d4 = array(1,2,3,4,5);
$_SESSION['array1'] = array($d1,$d2,$d3,$d4);
unset($_SESSION['array1'][1]);
print_R($_SESSION);
Prints:
Array
(
[array1] => Array
(
[0] => Array
(
...
)
[2] => Array
(
...
)
[3] => Array
(
...
)
)
)
So some debugging ideas:
@session_start
.error_reporting(E_ALL)
Upvotes: 1
Reputation: 914
How about storing your session variable again:
$_SESSION['array1'] = array($d1,$d3,$d4);
Upvotes: 0