Reputation: 3311
I have a session that contains an array. The array contains the following data:
Array (
[0] => /assets/img/user_photos/thumbs/9c2310c2def9981221ec37cbbafe0370.jpg
[1] => /assets/img/user_photos/thumbs/286b59eb3dafe2e0cf0df50e45f10250.jpg
[2] => /assets/img/user_photos/thumbs/4e6012cc396252594d2a05850b0a35ae.jpg
[3] => /assets/img/user_photos/thumbs/49ce9031319203c1911c0b9789a83ffc.jpg
[4] => /assets/img/user_photos/thumbs/da21379f3dc80541a087e1c4db5f929a.jpg
[5] => /assets/img/user_photos/thumbs/1f46378fdd7dcf7fda580e50ca92a2d0.jpg
)
I would like to delete an item from this array. How is this possible when the array is stored in a session?
Upvotes: 2
Views: 6533
Reputation: 421
You can use
unset($_SESSION['Array_name']['index_tobe_delete']);
OR
$_SESSION['Array_name']['index_tobe_delete'] = "" ;
Upvotes: 2
Reputation: 2981
in a non-hacked Environment the superglobal-Array $_SESSION references all data in the session. So you could delete an entry by this:
unset($_SESSION['indexToYourArray'][0]);
(you didn't mention in which session variable your index is stored). If the array is the session content the code should read:
unset($_SESSION[0]);
Upvotes: 2
Reputation: 7688
Use unset
<?php
unset($_SESSION['array'][0]);
var_dump($_SESSION);
?>
Upvotes: 1
Reputation: 27866
use unset to delete elements from an array.
unset($array[1]);
Upvotes: 7
Reputation: 3608
You can use unset()
Eg:
$_SESSION['abc'] = Array ('foo','bar');
to delete bar
:
unset($_SESSION['abc'][1]);
Upvotes: 1