2

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?

jprofitt
  • 10,802
  • 4
  • 34
  • 45
hairynuggets
  • 3,081
  • 21
  • 54
  • 88

8 Answers8

7

use unset to delete elements from an array.

unset($array[1]);
Elzo Valugi
  • 25,880
  • 14
  • 91
  • 114
2

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]);
Dau
  • 8,078
  • 4
  • 22
  • 47
pscheit
  • 2,274
  • 23
  • 28
2

You can use

unset($_SESSION['Array_name']['index_tobe_delete']);

OR

$_SESSION['Array_name']['index_tobe_delete'] = "" ;
1

You can use unset()

Eg:

$_SESSION['abc'] =  Array ('foo','bar');

to delete bar:

unset($_SESSION['abc'][1]);
Zul
  • 3,630
  • 3
  • 20
  • 35
1

Use unset

<?php
unset($_SESSION['array'][0]);
var_dump($_SESSION);
?>
Alex
  • 7,330
  • 20
  • 78
  • 147
1

You could unset the array item:

unset($_SESSION['array'][0]);
h00ligan
  • 1,461
  • 9
  • 17
0

use this

$array = array(0, 1, 2, 3);

unset($array[2]);
$array = array_values($array);
var_dump($array);

and for more info read this

Community
  • 1
  • 1
Dau
  • 8,078
  • 4
  • 22
  • 47
0
unset($_SESSION['array_name']);
animuson
  • 52,378
  • 28
  • 138
  • 145
Mark
  • 1,684
  • 3
  • 26
  • 43