【问题标题】:How do I access the members in a stdClass array of stdClasses?如何访问 stdClass 的 stdClass 数组中的成员?
【发布时间】:2012-03-10 17:39:48
【问题描述】:

我想知道如何访问 stdObject 的 stdObect 数组。使用print_r() 打印时,我有一个看起来像这样的数组:

stdClass Object ([item] => Array(
    [0] => stdClass Object([id] => 0 [name] => Peter)
    [1] => stdClass Object([id] => 1 [name] => Jack)))

如何访问名称字段?如果不是数组,我可以通过调用字段来获取属性,例如:

$client = new SoapClient("http://url.to.my.wsdl", array("trace" => 0));
echo $client->GetPerson()->name;

但是当使用数组时,这是行不通的:

$client = new SoapClient("http://url.to.my.wsdl", array("trace" => 0));
$persons = $client->GetPersons();
echo $persons[0]->name;

这只是给了我错误:

Fatal error: Cannot use object of type stdClass as array

【问题讨论】:

    标签: php arrays wsdl soap-client stdclass


    【解决方案1】:

    数组包含在字段item内。

    echo $persons->item[0]->name;
    

    【讨论】:

    • 谢谢!三个不同的人同时给出三个相同的答案 :-) 我会给声誉最低的人(@Lake)给出“正确答案”。希望没问题:)
    【解决方案2】:

    试试这个:

    $persons->item[0]->name;
    

    【讨论】:

    • 谢谢!三个不同的人同时给出三个相同的答案 :-) 我会给声誉最低的人(@Lake)给出“正确答案”。希望没关系:)
    【解决方案3】:

    根据你的 print_r 信息,试试吧

    echo $persons->item[0]->name
    

    【讨论】:

      猜你喜欢
      • 2014-01-22
      • 2011-06-07
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      相关资源
      最近更新 更多