php period in object name [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php object attribute with dot in name
I'm dealing with PHP, getting an object returned by a Microsoft web service, and there is a period in the object name!
object(stdClass)#22 (1) {
["DAE.Country"]=>
array(24) {
[0]=>
object(stdClass)#23 (2) {
["CountryName"]=>
string(4) "Asia"
["ID"]=>
string(2) "27"
}
}
}
How do I access an object in PHP with a period in its name?
$response->DAE_GetCountryListResult->DAE.Country;
and
$response->DAE_GetCountryListResult-['DAE.Country'];
both fail. Thank you for your time.

You can use this syntax to access the property you want:
$obj->{'DAE.Country'}
You can also use a variable and expressions inside the braces:
$prefix = 'DAE';
$name = 'Country';
$another_obj = $obj->{"$prefix.$name"};

Related

Extract a string from an object inside an array: PHP [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I'm writing a PHP plugin and need to extract a string from an object which is inside an array.
var_dump($data)
outputs:
array(1) {
[0]=> object(stdClass)#380 (53) {
["id"]=> string(1) "2"
["firstname"]=> string(6) "John"
["lastname"]=> string(6) "Doe"
["email"]=> string(31) "johndoe#email.com"
}
}
I want to return:
johndoe#mail.com
My research has turned up functions such as unserialize, array_map, array_slice, and array_column, but I haven't found the right function or combination of functions that work together (nor do I understand them enough) to just return a simple string from an object inside an array. Can anyone provide me with some guidance?
Edit: This isn't the same question as the one referenced as "the exact same question." The other one asks simply how to access an array (answer: $array[0]), and my question asked how to access an object INSIDE an array (answer: $array[0]->text).
For example:
$data[0]->firstname
Maybe:
$arr = get_object_vars($data[0]);
$arr['firstname'];
Example:
$obj = (object) array('x' => 'foo');
$arr = [$obj];
print($arr[0]->x);
You will get: foo

How to access array within PHP object [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I know this question is a little n00b but I am struggling to work out to how to access the array 'ta[]' within the PHP object below. Usually I would have no problem but because my key contains the brackets i.e. 'ta[]' I can't wrap my head around how to access it, I guess I somehow need to escape it..?
I have tried most combinations such as..
object->ta[]
object["ta[0]"]
object["ta[]"]
object->ta[0]
Any help welcome!
object(stdClass)#6 (11) {
["tc"]=> string(4) "4500"
["tct"]=> string(1) "1"
["pd"]=> string(2) "AT"
["df"]=> string(10) "08/04/2016"
["dt"]=> string(10) "08/08/2016"
["nt"]=> string(1) "2"
["ta[]"]=> array(2)
{
[0]=> string(2) "40"
[1]=> string(2) "35"
}
["rc"]=> string(2) "US"
["rs"]=> string(2) "AR"
["cc"]=> string(2) "US"
["dfp"]=> string(10) "07/30/2016"
}
This should do it
$obj->{"ta[]"};
Brace notation (using {}) does the same thing for accessing object properties as bracket notation (using []) does for accessing array keys: It lets you define the property name as an expression.
Which in this case is just a simple string but could could be any other expression. To prove that with a (silly) example:
function ta() {
return 'ta';
}
$obj = new stdClass;
$obj->{ta() . '[]'} = ['a', 'b'];
echo $obj->{"ta[]"}[1]; // b

How do I acces this property/element of this object/array? [duplicate]

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 6 years ago.
I'm trying to access a property of a stdClass but it's named after an index or something? I'm at a loss here, I would really appreciate some insight into how I can get at my data.
Thanks in advance,
object(stdClass)#79 (96) {
["0"]=> <------this is what I'm trying to access
array(1) {
[0]=>
object(stdClass)#80 (5) {
["m_id"]=>
string(3) "422"
["klplan_id"]=>
string(3) "945"
["m_naam"]=>
string(62) "opsporen lekkage vanuit badkamer/kitten bad rand /parkeren/kit"
["m_aantal"]=>
string(1) "1"
["m_prijs"]=>
string(4) "7.25"
}
}
This should do it:
$class->{'0'}

How get a value of my array in php? [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
This is my array:
array(3) {
["formData"]=>
array(25) {
["Contact.Name.First"]=>
object(stdClass)#17 (2) {
["value"]=>
string(31) "POLIANA KRUSCHER PISCOLLE"
["required"]=>
bool(true)
}
["Contact.CustomFields.c.new_cpf"]=>
object(stdClass)#21 (2) {
["value"]=>
string(14) "038.889.971-99"
["required"]=>
bool(true)
}
}
How can I retrieve value in Contact.CustomFields.c.new_cpf?
I tried $incident_data['Contact.CustomFields.c.new_cpf']['value'], but it returns null.
Try this way:
$incident_data['formData']['Contact.CustomFields.c.new_cpf']->value;
Your $incident_data['formData']['Contact.CustomFields.c.new_cpf'] does not contain an array yet you try to access it as such.
Since there is no such key php defaults to null. (Yet there also should be an undefined index notice thrown around. You may want to enhance your error logging / strictness level).
As by your dump you have an of the \stdClass class, you have to treat them as such:
$object->value

How do I parse a PHP object when the attribute name is *? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Special characters in property name of object
I'm parsing a JSON response from MediaWiki's API: Requested URL
I run it through json_decode() and then pull some pieces of it. The problem I have is that the attribute name for the content is *. I get a PHP error when I try to access the content ( the 140,950 character string at the end of my vardumped json_decoded example below ) like this:
foreach( $Response->query->pages as $page_id => $Page ) {
$this->id = $Page->pageid;
$this->title = $Page->title;
$this->content_with_markup = $Page->revisions[0]->*;
}
The PHP Error: PHP Parse error: syntax error, unexpected '*'
The pageid and title work fine.
Here is the piece of JSON_Decoded object that is giving me problems:
object(stdClass)#5 (1) {
["11968"]=>
object(stdClass)#6 (4) {
["pageid"]=>
int(11968)
["ns"]=>
int(0)
["title"]=>
string(17) "George Washington"
["revisions"]=>
array(1) {
[0]=>
object(stdClass)#7 (2) {
["contentformat"]=>
string(11) "text/x-wiki"
["*"]=>
string(140950) "{{About|the first President of the United States|other uses}}...
How do I access the content contained in the attribute named *?
Same as always.
...->{'*'}->...

Categories