Dysan Posted November 23, 2007 Share Posted November 23, 2007 How do I search for an item within an array, then delete it? Quote Link to comment Share on other sites More sharing options...
boushley Posted November 23, 2007 Share Posted November 23, 2007 Well... you could pass something in the url such as example.php?action=add&id=1 or example.php?action=del&id=1... or you could just pass an id and if it exists delete it and if it doesn't add it. Depends on what its for and what you want it to do... but the code would look something like this... <?php $id = $_GET['id']; $action = $_GET['action']; switch($action){ case 'add': $myArray[$id] = something; break; case 'del': unset($myArray[$id]); break; } ?> Quote Link to comment Share on other sites More sharing options...
boushley Posted November 23, 2007 Share Posted November 23, 2007 LOL... you should have just reposted... you completley changed your question... Do you want to search for a value... or an id? I'll assume you are wanting to pass an id and if it is there then delete it, if not create it. <?php $id = $_GET['id']; if(isset($myArray[$id])){ unset($myArray[$id]); } else { $myArray[$id] = something; } ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted November 23, 2007 Share Posted November 23, 2007 <?php //delete all the array values that match $search function filter_array ($array, $search) { foreach(array_keys($array, $search) as $k) unset ($array[$k]); return $array; } ?> Orio. 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.