Irfana
Irfana

Reputation: 231

Issue accessing objects within objects in a file with XML format

I have a XML format file which has objects inside objects. This is how I get values of first object, which works fine for me:

$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;

Now my question is how to access the object which is inside object. For example, how to get "BookID" which is 4 and how to get value of "Type" which is 1?

Any suggestion would be appreciated. Here is the object:

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
                                                )
                                        )
    ))))

Upvotes: 0

Views: 25

Answers (1)

BrainStone
BrainStone

Reputation: 3185

You do it like this:

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

If you would like to have an array containing all book ids you would have something like this:

$BookIDs = array();

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

Or if you would like to be able to modify the values in $response using the $BookIDs (as an example) iot would look like this:

$BookIDs = array();

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

Upvotes: 1

Related Questions