I iterate through an array of objects and want to delete one of the objects based on it's 'id' property, but my code doesn't work. foreach($array as $element) { foreach($element as $key => $value) { if($key == 'id' && $value == 'searched_value') { //delete this particular object from the $array unset($element);//this doesn't work unset($array,$element);//neither does this } } } Suggest me !
try this. foreach($array as $key1=>$element) { foreach($element as $key => $value) { if($key == 'id' && $value == 'searched_value') { //delete this particular object from the $array unset($array[$key1]); } } } PHP: and please use the [ PHP ] [/ PHP ] tags ( remove the spaces) when posting code so it's easier to read.
foreach($array as $elementKey => $element) { foreach($element as $valueKey => $value) { if($valueKey == 'id' && $value == 'searched_value'){ //delete this particular object from the $array unset($array[$elementKey]); } } } Lagna, I think this should be correct.