1

I need to remove an object from an array in php but I need the object reference to still exist, when I tried using unset() as described here it unset both the array element and the object itself, how can I remove the reference from the array without destroying the object?

my code looks like the following:

$this->array[id] = myobject;
unset($this->array[$id]);
5
  • 1
    set the array element to null rather than unset it or you can array pop the id too i guess Commented Jul 26, 2013 at 17:17
  • 1
    @Dave that won't make any difference. The key is whether there are any other variables pointing to that object. Commented Jul 26, 2013 at 17:20
  • doesn't he want to keep the object and the variables just remove the object from the array so instead of it being array($object1,$object2) it just becomes array($object2) in effect Commented Jul 26, 2013 at 17:28
  • According to php manual unset($arr[5]); // This removes the element from the array so in your case unset($this[$id]); should remove the item from the $this array (badly define object array using $this as the array name). can you post your proper code not the example above lets see the whole class and constructs etc so we can see what exactly your trying to do. I suspect its not an unset() issue but more of your syntax issue Commented Jul 26, 2013 at 17:38
  • $this[id] doesn't really work. $this is preoccupied. Do you mean $this->array[id] = myobject;? Commented Jul 26, 2013 at 17:43

3 Answers 3

2

This code works just fine for me:

$my_var = 1;
$my_array = array();
$id = 0;

$my_array[$id] = $my_var;
print_r($my_array);
print_r($my_var);

unset($my_array[$id]);
print_r($my_array);
print_r($my_var);

At the end of it all, I have cleared the $id (0) index from $my_array, by $my_var is still equal to 1.

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

1 Comment

upon further debugging and looking at a lot of var_dump()'s i realized that the unset was not the problem. it was that the value was not the type i expected when i populated it. thank you for your answer anyway though
1

You need another reference for this object. Look at this code:

<?php
$var = 1;

$data = array("id" => &$var);
unset($data['id']);

var_dump($var);
var_dump($data);

It prints:

int(1)
array(0) {
}

Just keep another reference to this object somewhere else.

Comments

1

Have you tried to keep a reference of the object before destroy array reference?

$user = new User('name','surname');
$myUserReference = $user;
$data = array('user'=> $user);

print_r($data);
unset($data['user']);
print_r($data);

This should print an array containing $user object and an empty array then. You should have a reference to $user object in $myUserReference var

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.