How do I access the members in a stdClass array of stdClasses?
-
28-10-2019 - |
문제
I am wondering how I can access a stdObect array of stdObjects. I have an array that looks something like this when printing it with print_r()
:
stdClass Object ([item] => Array(
[0] => stdClass Object([id] => 0 [name] => Peter)
[1] => stdClass Object([id] => 1 [name] => Jack)))
How can I access the name field? If it not was an array, I could get the attributes by calling the field, like:
$client = new SoapClient("http://url.to.my.wsdl", array("trace" => 0));
echo $client->GetPerson()->name;
But when using arrays, this does not work:
$client = new SoapClient("http://url.to.my.wsdl", array("trace" => 0));
$persons = $client->GetPersons();
echo $persons[0]->name;
That just gives me the error:
Fatal error: Cannot use object of type stdClass as array
해결책
According to your print_r info ,just try
echo $persons->item[0]->name
다른 팁
try this:
$persons->item[0]->name;
The array is contained inside the field item
.
echo $persons->item[0]->name;
제휴하지 않습니다 StackOverflow