284

If I have an stdObject say, $a.

Sure there's no problem to assign a new property, $a,

$a->new_property = $xyz;

But then I want to remove it, so unset is of no help here.

So,

$a->new_property = null;

is kind of it. But is there a more 'elegant' way?

YakovL
  • 7,557
  • 12
  • 62
  • 102
valk
  • 9,363
  • 12
  • 59
  • 79

5 Answers5

502
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())
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • 10
    n.b. It is not possible to directly unset a property that has an integer key. e.g. `$o=(object)array('a','b','c');` (You have to convert the object (back) to an array even to simply _access_ such properties!) – danorton Mar 19 '14 at 22:09
  • @danorton, the question is about an `object`, not an `array`. For an array, you need to [splice](http://ca3.php.net/array_splice) it. – Yanick Rochon Mar 20 '14 at 01:00
  • 5
    @YanickRochon, my comment is about an `object`, not an `array`. ;-) – danorton Mar 21 '14 at 02:39
  • 2
    @danorton why would someone, in their right mind, convert an `array` into an `object`? It just makes no sense (even if PHP allows it). I will not encorage and spread bad programming habits by commenting on this :) No offense. – Yanick Rochon Mar 21 '14 at 02:54
  • 8
    @YanickRochon this can make sense if you want to merge the properties of two objects (that have no method, they just are used as storage medium): in this case, the fastest and simplest route is to convert both objects into arrays to apply array_replace_recursive() and back onto objects afterwards. As to why someone would use an object to only store properties, this often happens in applications fetching from databases such as MySQL the records as an object instead of an array (using standard functions such as mysql_fetch_object()). – gaborous Jan 21 '15 at 20:37
  • 2
    @danorton FYI, since 7.2 you can do `unset($o->{0});` or `$k = 0; unset($o->$k);`. – Sarke Jun 12 '20 at 04:20
65

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})
Greg
  • 5,862
  • 1
  • 25
  • 52
Sajjad Ashraf
  • 3,754
  • 1
  • 34
  • 35
  • 1
    Could you specify which "newer" versions are described? For me, with PHP 7.1 `unset($object[$key])` works and `unset($object->{$key})` does not (to be more precise, `unset($object['literal_key'])` and `unset($object->{'literal_key'})`). PS Hm, I may be wrong, though, in thinking the thing I'm editing is an object, not an array – YakovL Jan 05 '19 at 13:28
  • Does not unset a property from a collection of objects. – Viktor Joras Aug 15 '19 at 17:14
3

This also works if you are looping over an object.

unset($object->$key);

No need to use brackets.

dandyboh
  • 117
  • 1
  • 2
1

This code is working fine for me in a loop

$remove = array(
    "market_value",
    "sector_id"
);

foreach($remove as $key){
    unset($obj_name->$key);
}
Ashiq Dey
  • 309
  • 5
  • 18
  • 3
    Your answer uses `array` but the question was about `stdClass` objects. What you said isn't wrong, but irrelevant to the question. – Neek Mar 12 '21 at 05:03
1

Set an element to null just set the value of the element to null the element still exists

unset an element means remove the element it works for array, stdClass objects user defined classes and also for any variable

<?php
    $a = new stdClass();
    $a->one = 1;
    $a->two = 2;
    var_export($a);
    unset($a->one);
    var_export($a);
    
    class myClass
    {
        public $one = 1;
        public $two = 2;
    }
    
    $instance = new myClass();
    var_export($instance);
    unset($instance->one);
    var_export($instance);
    
    $anyvariable = 'anyValue';
    var_export($anyvariable);
    unset($anyvariable);
    var_export($anyvariable);
TexWiller
  • 318
  • 2
  • 9