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.
More Related Contents:
- How can I access an array/object?
- How to get WooCommerce order details
- How to access object properties with names like integers or invalid property names?
- Property Property Property [title] does not exist on this collection instance does not exist on this collection instance does not exist on this collection instance
- Calling closure assigned to object property directly
- Get PHP class property by string
- How can I access a property with an invalid name?
- Initializing PHP class property declarations with simple expressions yields syntax error
- What is the syntax for accessing PHP object properties? [closed]
- Accessing Class Properties with Spaces
- Get a PHP object property that is a number [duplicate]
- Getting static property from a class with dynamic class name in PHP
- Constant expression contains invalid operations [duplicate]
- Magic __get getter for static properties in PHP
- How to create new property dynamically
- Access object property with disallowed character in property name
- How can I access object properties with names like integers or invalid property names?
- How can I loop through a MySQL result set more than once using the mysql_* functions?
- PHP json_decode() returns NULL with valid JSON?
- instantiate a class from a variable in PHP?
- How do I get Greenwich Mean Time in PHP?
- Get user’s current location
- PHP curly braces in array notation
- Is there a way to show a WHERE clause just for one field in MySQL?
- Warning: mysqli_query() expects parameter 1 to be mysqli, string given in
- send email with attachment using php
- Does PHP optimize tail recursion?
- Set custom shipping rates programmatically in Woocommerce 3
- phpMailer – How do you Remove Recipients
- adding a namespace when using SimpleXMLElement