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]; }
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!