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,874
  • 4
  • 36
  • 46
hairynuggets
  • 3,191
  • 22
  • 55
  • 90

8 Answers8

7

use unset to delete elements from an array.

unset($array[1]);
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 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,578
  • 4
  • 23
  • 48
pscheit
  • 2,882
  • 27
  • 29
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,627
  • 3
  • 21
  • 35
1

Use unset

<?php
unset($_SESSION['array'][0]);
var_dump($_SESSION);
?>
Alex
  • 7,538
  • 23
  • 84
  • 152
1

You could unset the array item:

unset($_SESSION['array'][0]);
h00ligan
  • 1,471
  • 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,578
  • 4
  • 23
  • 48
0
unset($_SESSION['array_name']);
animuson
  • 53,861
  • 28
  • 137
  • 147
Mark
  • 1,754
  • 3
  • 26
  • 43