unset($a->new_property);
This works for array elements, variables, and object attributes.
Example:
$a = new stdClass();
$a->new_property = 'foo';
var_export($a); // -> stdClass::__set_state(array('new_property' => 'foo'))
unset($a->new_property);
var_export($a); // -> stdClass::__set_state(array())
Answer from Yanick Rochon on Stack Overflowunset($a->new_property);
This works for array elements, variables, and object attributes.
Example:
$a = new stdClass();
$a->new_property = 'foo';
var_export($a); // -> stdClass::__set_state(array('new_property' => 'foo'))
unset($a->new_property);
var_export($a); // -> stdClass::__set_state(array())
This also works specially if you are looping over an object.
unset($object[$key])
Update
Newer versions of PHP throw fatal error Fatal error: Cannot use object of type Object as array as mentioned by @CXJ . In that case you can use brackets instead
unset($object->{$key})
No, you do not need to delete an object after its use in PHP. Let PHP's garbage collection take care of it for you.
There may be some obscure performance reasons to unset an object after you are done with it in very specific situations. But such cases are not everyday events, and they will be clear when you see them.
PHP's GC was hit and miss prior to 5.3 with many arrays and objects becoming unfreeable until the script ended. Because of this, PHP development basically took on an unspoken “who needs GC anyways” attitude because vars were only guaranteed to be freed when the script ended.
Two things are happening which change this:
First, PHP's GC has advanced to where its only unreliable with arrays that contain copies of other arrays. PHP's GC will be totally functional and reliable once this issue is fixed .
Secondly, PHP's pthread extension is coming along pretty well. It's very likely that PHP will have full thread support within the next couple years, making long running scripts as usefull as many Java appliations.
These two things are likely to change many standards in the way PHP is used. For example, your already starting to see serious efforts to create PHP based network services (such as Ratchet) now that the GC is reliable under most circumstances.
So to make a long story short - no, your currently not required to unset any form of resources in PHP because they will all be freed when the script ends. However, unsetting objects and other resources will make sure that each unneeded resource is freed before the script ends, potentially freeing up highly contented resources and thus improve performance.
Moreover, the practice of unsetting objects and resources is likely to become the norm in the near future, and likely an absolute requirement in many cases.
Note that many PHP objects/resources don't fall out of scope until the script ends. This means that unset() is the only way to ensure that their ref count is set to 0 in order to become a candidate for freeing by the GC (before the script ends, that is.)
You're looking for unset().
But take into account that you can't explicitly destroy an object.
It will stay there, however if you unset the object and your script pushes PHP to the memory limits the objects not needed will be garbage collected. I would go with unset() (as opposed to setting it to null) as it seems to have better performance (not tested but documented on one of the comments from the PHP official manual).
That said, do keep in mind that PHP always destroys the objects as soon as the page is served. So this should only be needed on really long loops and/or heavy intensive pages.
A handy post explaining several mis-understandings about this:
Don't Call The Destructor explicitly
This covers several misconceptions about how the destructor works. Calling it explicitly will not actually destroy your variable, according to the PHP5 doc:
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.
The post above does state that setting the variable to null can work in some cases, as long as nothing else is pointing to the allocated memory.
array_splice($objectarray, $index, 1);
//array_splice accepts 3 parameters (array, start, length) and removes the given
//array and then normalizes the index
//OR
unset($objectarray[$index]); //removes the array at given index
$reindex = array_values($objectarray); //normalize index
$objectarray = $reindex; //update variable
You have to use the function unset on your array.
So its like that:
<?php
$index = 2;
$objectarray = array(
0 => array('label' => 'foo', 'value' => 'n23'),
1 => array('label' => 'bar', 'value' => '2n13'),
2 => array('label' => 'foobar', 'value' => 'n2314'),
3 => array('label' => 'barfoo', 'value' => '03n23')
);
var_dump($objectarray);
foreach ($objectarray as $key => $object) {
if ($key == $index) {
unset($objectarray[$index]);
}
}
var_dump($objectarray);
?>
Remember, your array will have odd indexes after that and you must (if you want) reindex it.
$foo2 = array_values($objectarray);