How to delete an element from an array of objects in php ?
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?
First find the index of your object (by iterating over the array - or binary searching), and then unset the array at that index.
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
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.
精彩评论