1

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?

1
  • 1
    Roll a die. Seriously, what do you know about the object that must be deleted? Commented Sep 16, 2011 at 8:55

3 Answers 3

1

First find the index of your object (by iterating over the array - or binary searching), and then unset the array at that index.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.