mark-unna Posted January 11, 2008 Share Posted January 11, 2008 Hi all I think I'm being a idiot here, but my head has gone blank on the following. I have an array with words in it, such as: $fruits = array('Apple', 'Banana', 'Clementine', 'Damson' ...) Now how do I remove a specific array value if I don't know its key? For example, I don't know where Damson sits in the array (assume I have hundreds of elements in the array), so how do I remove it without knowing its index? Ideally I just want to: unset($fruits['Damson']), but this doesn't work. I know it's an obvious one, but after a good while of googling, I just can't find the answer! Help! Quote Link to comment Share on other sites More sharing options...
Daukan Posted January 11, 2008 Share Posted January 11, 2008 array_search() function will find the value and return the key. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 11, 2008 Share Posted January 11, 2008 Here's one way of doing it. <?php for ($f=0; $f<count($fruits); $f++) if ($fruits[$f] == "Damson") unset($fruits[$f]); ?> Quote Link to comment Share on other sites More sharing options...
mark-unna Posted January 11, 2008 Author Share Posted January 11, 2008 Thanks guys. Saved me loads of time! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.