279

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?

2
  • Can you post code how you tried with unset ? In mine sense unset should work.But possibly you duplicating the context
    – Arshdeep
    Aug 30, 2010 at 13:29
  • really not sure how unset() is more elegant than = null, but may be that's just me Jul 2, 2019 at 4:13

5 Answers 5

493
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())
6
  • 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, 2014 at 22:09
  • 5
    @YanickRochon, my comment is about an object, not an array. ;-)
    – danorton
    Mar 21, 2014 at 2: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. Mar 21, 2014 at 2: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, 2015 at 20:37
  • 2
    @danorton FYI, since 7.2 you can do unset($o->{0}); or $k = 0; unset($o->$k);.
    – Sarke
    Jun 12, 2020 at 4:20
63

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})
2
  • 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 5, 2019 at 13:28
  • Does not unset a property from a collection of objects. Aug 15, 2019 at 17:14
3

This also works if you are looping over an object.

unset($object->$key);

No need to use brackets.

1
  • 4
    This question does not add anything new in comparison with @YannickRochon's one.
    – helvete
    Mar 5, 2019 at 15:55
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);
}
1
  • 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, 2021 at 5: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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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