Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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]);
share|improve this question
1  
set the array element to null rather than unset it or you can array pop the id too i guess – Dave Jul 26 at 17:17
@Dave that won't make any difference. The key is whether there are any other variables pointing to that object. – jcsanyi Jul 26 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 – Dave Jul 26 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 – Dave Jul 26 at 17:38
$this[id] doesn't really work. $this is preoccupied. Do you mean $this->array[id] = myobject;? – Jim Martens Jul 26 at 17:43
show 1 more comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
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 – Nanos Jul 26 at 18:00

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.