How to Access a Deep Object Property Named as a Variable (Dot Notation) in PHP

How can I access a deep object property named as a variable (dot notation) in php?

It is very easy to reduce the object path using variable property notation ($o->$p):

$path = 'foo.bar';
echo array_reduce(explode('.', $path), function ($o, $p) { return $o->$p; }, $user);

This could easily be turned into a small helper function.

Accessing nested property on stdClass Object with string representation of the node

My initial searches didn't yield many results, however, after thinking up a broader range of search terms I found other questions on SO that addressed similar problems. I've come up with three solutions. All will work, but not all will work for everyone.

Solution 1 - Looping

Using an approach similar to the question referenced in my original question or the loop proposed by @miken32 will work.

Solution 2 - anonymous function

The string can be exploded into an array. The array can then be parsed using array_reduce() to produce the result. In my case, the working code (with a check for incorrect/non-existent property names/spellings) was this (PHP 7+):

//create object - this comes from and external API in my case, but I'll include it here 
//so that others can copy and paste for testing purposes

$obj = (object)[
'parent' => (object)[
'requestdata' => (object)[
'inputs' => (object)[
'firstname' => 'Travis'
]
]
]
];

//string representing the property we want to get on the object

$property = 'parent->requestdata->inputs->firstname';

$name = array_reduce(explode('->', $property), function ($previous, $current) {
return is_numeric($current) ? ($previous[$current] ?? null) : ($previous->$current ?? null); }, $obj);

var_dump($name); //outputs Travis

see this question for potentially relevant information and the code I based my answer on.

Solution 3 - symfony property access component

In my case, it was easy to use composer to require this component. It allows access to properties on arrays and objects using simple strings. You can read about how to use it on the symfony website. The main benefit for me over the other options was the included error checking.

My code ended up looking like this:

//create object - this comes from and external API in my case, but I'll include it here 
//so that others can copy and paste for testing purposes
//don't forget to include the component at the top of your class
//'use Symfony\Component\PropertyAccess\PropertyAccess;'

$obj = (object)[
'parent' => (object)[
'requestdata' => (object)[
'inputs' => (object)[
'firstname' => 'Travis'
]
]
]
];

//string representing the property we want to get on the object
//NOTE: syfony uses dot notation. I could not get standard '->' object notation to work.

$property = 'parent.requestdata.inputs.firstname';

//create symfony property access factory

$propertyAccessor = PropertyAccess::createPropertyAccessor();

//get the desired value

$name = $propertyAccessor->getValue($obj, $property);

var_dump($name); //outputs 'Travis'

All three options will work. Choose the one that works for you.

php dynamic property accessor for nested properties

Example

<?php

$v = new stdClass;
$v->x = new stdClass;
$v->x->y = new stdClass;
$v->x->y->z = "wow";

$keys = "x->y->z";

$t = &$v;
foreach (explode("->", $keys) as $key) {
// @TODO add isset($t->{$key})
$t = &$t->{$key};
}

print($t . PHP_EOL);

$t = "changed";

print($v->x->y->z . PHP_EOL);

Remember to use reference (&) when you want to edit this nested object.

Access object child properties using a dot notation string

Here's a naive function I wrote a while ago, but it works for basic object properties:

function getDescendantProp(obj, desc) {
var arr = desc.split(".");
while(arr.length && (obj = obj[arr.shift()]));
return obj;
}

console.log(getDescendantProp(r, "b.b2"));
//-> 99

Although there are answers that extend this to "allow" array index access, that's not really necessary as you can just specify numerical indexes using dot notation with this method:

getDescendantProp({ a: [ 1, 2, 3 ] }, 'a.2');
//-> 3

PHP stdClass Object with dot

You can try using braces:

$object->GetPlayerResult->{"RegistrationResponses.Player"}

Or you can cast it to an associative array

$result = (array) $object->GetPlayerResult;
$player = $result["RegistrationResponses.Player"];

If you are parsing your website response using json_decode, note the existence of the second parameter to return as associative array:

assoc


When TRUE, returned objects will be converted into associative arrays.

How can I access and process nested objects, arrays, or JSON?

Preliminaries

JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of object.

(Plain) Objects have the form

{key: value, key: value, ...}

Arrays have the form

[value, value, ...]

Both arrays and objects expose a key -> value structure. Keys in an array must be numeric, whereas any string can be used as key in objects. The key-value pairs are also called the "properties".

Properties can be accessed either using dot notation

const value = obj.someProperty;

or bracket notation, if the property name would not be a valid JavaScript identifier name [spec], or the name is the value of a variable:

// the space is not a valid character in identifier names
const value = obj["some Property"];

// property name as variable
const name = "some Property";
const value = obj[name];

For that reason, array elements can only be accessed using bracket notation:

const value = arr[5]; // arr.5 would be a syntax error

// property name / index as variable
const x = 5;
const value = arr[x];

Wait... what about JSON?

JSON is a textual representation of data, just like XML, YAML, CSV, and others. To work with such data, it first has to be converted to JavaScript data types, i.e. arrays and objects (and how to work with those was just explained). How to parse JSON is explained in the question Parse JSON in JavaScript? .

Further reading material

How to access arrays and objects is fundamental JavaScript knowledge and therefore it is advisable to read the MDN JavaScript Guide, especially the sections

  • Working with Objects
  • Arrays
  • Eloquent JavaScript - Data Structures


Accessing nested data structures

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.

Here is an example:

const data = {
code: 42,
items: [{
id: 1,
name: 'foo'
}, {
id: 2,
name: 'bar'
}]
};

Let's assume we want to access the name of the second item.

Here is how we can do it step-by-step:

As we can see data is an object, hence we can access its properties using dot notation. The items property is accessed as follows:

data.items

The value is an array, to access its second element, we have to use bracket notation:

data.items[1]

This value is an object and we use dot notation again to access the name property. So we eventually get:

const item_name = data.items[1].name;

Alternatively, we could have used bracket notation for any of the properties, especially if the name contained characters that would have made it invalid for dot notation usage:

const item_name = data['items'][1]['name'];

I'm trying to access a property but I get only undefined back?

Most of the time when you are getting undefined, the object/array simply doesn't have a property with that name.

const foo = {bar: {baz: 42}};
console.log(foo.baz); // undefined

Use console.log or console.dir and inspect the structure of object / array. The property you are trying to access might be actually defined on a nested object / array.

console.log(foo.bar.baz); // 42

What if the property names are dynamic and I don't know them beforehand?

If the property names are unknown or we want to access all properties of an object / elements of an array, we can use the for...in [MDN] loop for objects and the for [MDN] loop for arrays to iterate over all properties / elements.

Objects

To iterate over all properties of data, we can iterate over the object like so:

for (const prop in data) {
// `prop` contains the name of each property, i.e. `'code'` or `'items'`
// consequently, `data[prop]` refers to the value of each property, i.e.
// either `42` or the array
}

Depending on where the object comes from (and what you want to do), you might have to test in each iteration whether the property is really a property of the object, or it is an inherited property. You can do this with Object#hasOwnProperty [MDN].

As alternative to for...in with hasOwnProperty, you can use Object.keys [MDN] to get an array of property names:

Object.keys(data).forEach(function(prop) {
// `prop` is the property name
// `data[prop]` is the property value
});

Arrays

To iterate over all elements of the data.items array, we use a for loop:

for(let i = 0, l = data.items.length; i < l; i++) {
// `i` will take on the values `0`, `1`, `2`,..., i.e. in each iteration
// we can access the next element in the array with `data.items[i]`, example:
//
// var obj = data.items[i];
//
// Since each element is an object (in our example),
// we can now access the objects properties with `obj.id` and `obj.name`.
// We could also use `data.items[i].id`.
}

One could also use for...in to iterate over arrays, but there are reasons why this should be avoided: Why is 'for(var item in list)' with arrays considered bad practice in JavaScript?.

With the increasing browser support of ECMAScript 5, the array method forEach [MDN] becomes an interesting alternative as well:

data.items.forEach(function(value, index, array) {
// The callback is executed for each element in the array.
// `value` is the element itself (equivalent to `array[index]`)
// `index` will be the index of the element in the array
// `array` is a reference to the array itself (i.e. `data.items` in this case)
});

In environments supporting ES2015 (ES6), you can also use the for...of [MDN] loop, which not only works for arrays, but for any iterable:

for (const item of data.items) {
// `item` is the array element, **not** the index
}

In each iteration, for...of directly gives us the next element of the iterable, there is no "index" to access or use.


What if the "depth" of the data structure is unknown to me?

In addition to unknown keys, the "depth" of the data structure (i.e. how many nested objects) it has, might be unknown as well. How to access deeply nested properties usually depends on the exact data structure.

But if the data structure contains repeating patterns, e.g. the representation of a binary tree, the solution typically includes to recursively [Wikipedia] access each level of the data structure.

Here is an example to get the first leaf node of a binary tree:

function getLeaf(node) {
if (node.leftChild) {
return getLeaf(node.leftChild); // <- recursive call
}
else if (node.rightChild) {
return getLeaf(node.rightChild); // <- recursive call
}
else { // node must be a leaf node
return node;
}
}

const first_leaf = getLeaf(root);

const root = {    leftChild: {        leftChild: {            leftChild: null,            rightChild: null,            data: 42        },        rightChild: {            leftChild: null,            rightChild: null,            data: 5        }    },    rightChild: {        leftChild: {            leftChild: null,            rightChild: null,            data: 6        },        rightChild: {            leftChild: null,            rightChild: null,            data: 7        }    }};function getLeaf(node) {    if (node.leftChild) {        return getLeaf(node.leftChild);    } else if (node.rightChild) {        return getLeaf(node.rightChild);    } else { // node must be a leaf node        return node;    }}
console.log(getLeaf(root).data);

Accessing the value of a property with a known name that is nested in a JSON object with vanilla JavaScript

If there is only one key, you could get the first element with Object.keys of data.query.pages.

var data = {        "batchcomplete": "",        "query": {            "normalized": [              {                  "from": "Theodore_Rubin",                  "to": "Theodore Rubin"              }            ],            "pages": {                "11820415": {                    "pageid": 11820415,                    "ns": 0,                    "title": "Theodore Rubin",                    "contentmodel": "wikitext",                    "pagelanguage": "en",                    "pagelanguagehtmlcode": "en",                    "pagelanguagedir": "ltr",                    "touched": "2016-02-12T17:34:52Z",                    "lastrevid": 138813300,                    "length": 34,                    "redirect": "",                    "new": "",                    "fullurl": "https://en.wikipedia.org/wiki/Theodore_Rubin",                    "editurl": "https://en.wikipedia.org/w/index.php?title=Theodore_Rubin&action=edit",                    "canonicalurl": "https://en.wikipedia.org/wiki/Theodore_Rubin"                }            }        }    };
console.log(data.query.pages[Object.keys(data.query.pages)[0]].fullurl);

Accessing numerical property in a json object

As well as using the dot notation, you can use regular array notation to access JSON nodes:

some_array.und['0'].value


Related Topics



Leave a reply



Submit