hairynuggets
hairynuggets

Reputation: 3311

How do I delete an item in an array that is in a session? php

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

Answers (8)

Mark
Mark

Reputation: 1852

unset($_SESSION['array_name']);

Upvotes: 0

Vishnu Choudhary
Vishnu Choudhary

Reputation: 421

You can use

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

OR

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

Upvotes: 2

pscheit
pscheit

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

h00ligan
h00ligan

Reputation: 1481

You could unset the array item:

unset($_SESSION['array'][0]);

Upvotes: 1

Alex
Alex

Reputation: 7688

Use unset

<?php
unset($_SESSION['array'][0]);
var_dump($_SESSION);
?>

Upvotes: 1

Elzo Valugi
Elzo Valugi

Reputation: 27866

use unset to delete elements from an array.

unset($array[1]);

Upvotes: 7

Zul
Zul

Reputation: 3608

You can use unset()

Eg:

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

to delete bar:

unset($_SESSION['abc'][1]);

Upvotes: 1

Dau
Dau

Reputation: 8848

use this

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

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

and for more info read this

Upvotes: 0

Related Questions