remove_property

Remove one or more properties from an object

Definition

object remove_property(object $object, string|array[string] $key)

Returns an object with all the properties of $object except $key. If $key doesn’t exist in $object, returns $object as is.

Parameters

object $object

Object inside which to remove the property.

string|array[string] $key

Key(s) of properties inside $object to remove. Can be a single string or an array of strings.

Returns

object

Returns $object without the properties in $key.

Examples

{
  "person": {
    "firstName": "Jane",
    "lastName": "Doe",
    "age": 30,
    "status": {
      "Enabled": true
    },
    "roles": [
      "User",
      "Admin"
    ]
  }
}
{
    objectWithoutAge: remove_property(person, 'age'),
    objectWithoutName: remove_property(person, ['firstName','lastName'])
}
{
  "objectWithoutAge": {
    "firstName": "Jane",
    "lastName": "Doe",
    "status": {
      "Enabled": true
    },
    "roles": [
      "User",
      "Admin"
    ]
  },
  "objectWithoutName": {
    "age": 30,
    "status": {
      "Enabled": true
    },
    "roles": [
      "User",
      "Admin"
    ]
  }
}