发出使用XML格式访问文件中对象内的对象的问题

时间:2016-03-15 11:06:34

标签: php web-services object

我有一个XML格式文件,其中包含对象内的对象。这就是我获取第一个对象的值的方法,这对我来说很好:

$soapclient = new SoapClient('http://anywb/book.asmx?WSDL');
$params = array('ISBN' => "1111");
$response = $soapclient->GetBookByISBN($params);       
//This will give me the value "Success" 
$result = $response->GetBookByISBNResult->ResponseText;

现在我的问题是如何访问对象内部的对象。例如,如何获得4的“BookID”以及如何获得“Type”的值为1?

任何建议都将不胜感激。这是对象:

stdClass Object
        (
            [GetBookByISBN] => stdClass Object
                (
                    [ResponseText] => Success
                    [SearchResult] => stdClass Object
                        (
                            [Search] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [Date] => 2015-10-20
                                            [BookID] => 4
                                            [Discription] => stdClass Object
                                                ( 
                                                    [Type] => 1
                                                )

                                            [Probability] => stdClass Object
                                                (
                                                    [Kids] => 
                                                    [Adult] => 00
                                                )

                                        )

                                    [1] => stdClass Object
                                        (
                                            [Date] => 2016-11-15
                                            [BookID] => 5
                                            [Discription] => stdClass Object
                                                ( 
                                                    [Type] => 2
                                                )

                                            [Probability] => stdClass Object
                                                (
                                                    [Kids] => 
                                                    [Adult] => 00
                                                )
                                        )
    ))))

1 个答案:

答案 0 :(得分:1)

你这样做:

$response->GetBookByISBN->SearchResult->Search[0]->BookID;
$response->GetBookByISBN->SearchResult->Search[0]->Discription->Type;

如果你想拥有一个包含所有图书标识的数组,你可以这样:

$BookIDs = array();

foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
  $BookIDs[$key] = $value->BookID;
}

或者,如果您希望能够使用$response修改$BookIDs中的值(作为示例),iot将如下所示:

$BookIDs = array();

foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
  $BookIDs[$key] = &$value->BookID;
}