php object attribute with dot in name
Specify aliases in your SQL query like SELECT column AS nameWithoutDots ...
or access these properties with $object->{'operation.name'}
or cast the object to array like this: $obj = (array)$obj; echo $obj['operation.name']
.
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 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.
Get json object's value in php if its name contains dots in PHP
$obj->root->{'hello.world'}
will work.
ps: $obj->root->{hello.world}
might not work.
b.t.w: Why not use Array? json_decode($json, true)
will return Array. Then $a['root']['hello.world']
will always work.
Is it possible to use period (dot) in a PHP function name?
the answer
to call a method in php, use ->
not .
(.) using in PHP
for combining two strings, like:
$str1 = "hello ";
$str2 = "world";
$str12 = $str1.$str2;
hello world
(.) using in js
It's called dot-notation, you can access properties on an object by specifying the name of the object, followed by a dot (period) followed by the property name. This is the syntax: objectName.propertyName;
How to convert dot notation to object in php?
class sth
{
public function convert($instance , $str)
{
$params = explode('.', $str);
if($params == 1) {
return $instance;
} else {
$obj = $instance;
foreach($params as $key => $param) {
if(!$key) {
continue;
}
$obj = $obj->{$param};
}
}
return $obj;
}
}
$obj = new stdClass();
$obj->test = new stdClass();
$obj->test->test2 = new stdClass();
$sth = new sth();
var_dump($sth->convert($obj, 'sth.test.test2'));
PHP generic function to implement dot notation to navigate into hierarchy of objects and retrieve a property
First, use a loop or recursion instead of a switch.
function getName(Foo $foo, string $path){
$components=explode('.', $path);
$current=$foo;
foreach($components as $p){
$current=$current->$p;
}
return $current->name;
}
I will further comment that that accessing arbitrary properties on objects(at least actual class instances, not StdClass
objects) seems like a poor design. Depending on the actual goal, you should be able to structure your classes to better support this(perhaps define an interface Nameable
that classes must implement, or perhaps move the name lookup into the class itself so that it is responsible for recursion). The ideal implementation is difficult to define without knowing the real use case.
Related Topics
Converting an Array from One to Multi-Dimensional Based on Parent Id Values
Round to Max Thousand, Hundred etc in PHP
How to Open a File from Line X to Line Y in PHP
Whats the Easiest Way to Determine If a User Is Online? (Php/Mysql)
When Do I Use Static Variables/Functions in PHP
Adding Three Months to a Date in PHP
Getting Static Property from a Class with Dynamic Class Name in PHP
Php: Setting a Timezone by Utc Offset
In PHP How to Inspect Content of a Zip File Without Extracting Its Content First
PHP Exec - Check If Enabled or Disabled
Changing Woocommerce Cart Item Names
Using Regex to Filter Attributes in Xpath with PHP
PHP Integer and Float Comparison Mismatch
Laravel Migration Table Already Exists, But I Want to Add New Not the Older