Dysan Posted November 23, 2007 Share Posted November 23, 2007 How do I search for an item within an array, then delete it? Link to comment https://forums.phpfreaks.com/topic/78584-delete-item-from-array/ 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; } ?> Link to comment https://forums.phpfreaks.com/topic/78584-delete-item-from-array/#findComment-397621 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; } ?> Link to comment https://forums.phpfreaks.com/topic/78584-delete-item-from-array/#findComment-397648 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. Link to comment https://forums.phpfreaks.com/topic/78584-delete-item-from-array/#findComment-397655 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.