November 28, 2010
Deleting an array element with PHP
Question by user478419
I have an array with several elements. The array keys are numeric.
I now want to remove a certain element. All I know is the element’s content, not the key.
Is there an easy way to remove this element from the array?
Or do I need to loop through all elements of the array to get the key and then unset the element with that key?
Answer by Codemwnci
You can use the array search function to get the key
$key = array_search('searchterm', $items);
unset($items[$key]);
Answer by Starx
sure
foreach($myarray as $key=>$item) {
if($item=='myknowncontent') { unset($myarray[$key]; }
}