Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Remove Objects From Array With Unset

Recommended Posts

Have been trying to solve this for a long time now, so I thought I´d give you guys a chance to break it!

I want to remove an object from an array that I store in a Session object. I´ve written a function that takes an array and an id as parameters. Checks if one of the objects has the id I want to remove and then it´s supposed to remove it. Quite simple, that´s why I cant see why it doesnt work!

Here´s the code:

function Remove_prod($id2,$AL){print_r($AL);foreach ($AL as $prod)if($prod->id == $id2){echo "REMOVING!<p><p>";unset($AL[$prod]);}print_r($AL);return $AL;}

I get the same data before and after the execution of the code. I know that the unset function is executed but no value is changed.

So, what say you?

Thanks in advance!
kvarnerexpress

Share this post


Link to post
Share on other sites

Note the line:

 

if($prod->id == $id2)

The '$prod' variable is not an object, yet here you are treating it as if it were. This would cause problems. I'm not sure exactly what 'id' is supposed to be, but if the array '$AL' contains arrays within itself, each with an inded 'id', try replacing '$prod->id' with '$prod['id']'.

 

It would help a lot if you could give an example of the structure of the '$AL' array. How many dimensions, is it numerically or associatively indexed, what is the data it contains, etc. A simple var_dump() would be sufficient.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.