سؤال

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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top