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! Link to comment https://forums.phpfreaks.com/topic/85523-solved-an-array-question/ 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. Link to comment https://forums.phpfreaks.com/topic/85523-solved-an-array-question/#findComment-436409 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]); ?> Link to comment https://forums.phpfreaks.com/topic/85523-solved-an-array-question/#findComment-436412 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! Link to comment https://forums.phpfreaks.com/topic/85523-solved-an-array-question/#findComment-436418 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.