1,105,208 Community Members

remove single element in session array

Member Avatar
anthonyjpv
Junior Poster in Training
98 posts since Oct 2010
Reputation Points: 6 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

Im building a shopping cart

for every product that is clicked, i append them to $_SESSION and put comma

so if producct 1 is clicked, the value of the $_SESSION is 1

then if producct 2 is clicked, the value of the $_SESSION becomes 1,2

and then I explode the array for me to get each element

my problem is

since I am storing this into session,

how to remove a single element from it?

so when I click delete say on product 2

the $_SESSION becomes 1 and not 1,2

thanks!

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,831 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
1
 

You can use unset on an array element to remove it from the array.

Member Avatar
blocblue
Practically a Posting Shark
828 posts since Jan 2008
Reputation Points: 238 [?]
Q&As Helped to Solve: 162 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

@pritaeas - unset requires you knowing the array index, which I'm guessing the OP will not know.

Another option would be to change the way you're storing items in the session to the following:

$_SESSION['product_id'] = array(
    // Product id => Quantity
    1 => 1,
    2 => 3,
);

Then you can do as @pritaeas says and use unset. You can also increment and decrement product quantities.

$product_id = 2;
unset($_SESSION['product_id'][$product_id]);
Member Avatar
anthonyjpv
Junior Poster in Training
98 posts since Oct 2010
Reputation Points: 6 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

I changed the way Im storing the product id to

array_push($_SESSION, $product_id);

then im doing the foreach to retrieve them instead of exploding array wih comma separation

now when I do the array_diff($array, $item_to_delete)

as in:

$_SESSION = array_diff($array, $item_to_delete);

it deletes all the array

Member Avatar
anthonyjpv
Junior Poster in Training
98 posts since Oct 2010
Reputation Points: 6 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
1
 

I found the answer to my question thanks for your help anyway!

i used array_search then I used the key to delete a certain value from it!

Member Avatar
blocblue
Practically a Posting Shark
828 posts since Jan 2008
Reputation Points: 238 [?]
Q&As Helped to Solve: 162 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

You would need to do:

$_SESSION['product_id'] = array_diff($array, array($item_to_delete));

And please try to use code tags.

Member Avatar
karthik_ppts
Posting Pro
574 posts since Feb 2011
Reputation Points: 81 [?]
Q&As Helped to Solve: 101 [?]
Skill Endorsements: 1 [?]
 
0
 
$index = array_search($remove_item,$_SESSION["product_id"]);
unset($_SESSION["product_id"][$index]);
Question Answered as of 2 Years Ago by blocblue, pritaeas and karthik_ppts
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article