How to delete an element from an array in php, where the array consists of objects and I don't know index of object that must be deleted?
3 Answers
You have to identify in some way your object.
Use a foreach to traverse your array without knowing your key and remove your object if you can match it in some way.
foreach($arr as &$val){
if($val == ...){ //whatever test you need to inditify your obj
unset($val);
break;
}
}
unset($val); // unset it again cause is a reference to your last traversed value
Comments
You can use this to see if an object is in an array.
function inArray($myObject, $array)
{
foreach($array as $object)
{
if($myObject === $$object)
return true;
}
return false;
}
You can transform this function how you like. This is basic knowledge.. I would recommend you to readup on some programming principles.