Designer
Designer

Reputation: 485

PHP stdClass Object - foreach

I'm using an API to get domain DNS details. The results printed show like this:

stdClass Object
(
    [test.co.uk] => stdClass Object
        (
            [records] => Array
                (
                    [0] => stdClass Object
                        (
                            [mname] => ns1.test.com.
                            [rname] => hostmaster.test.com.
                            [serial] => 12345678
                            [refresh] => 1800
                            [retry] => 900
                            [expire] => 1209600
                            [minimum-ttl] => 300
                            [ref] => 
                            [host] => test.co.uk
                            [type] => SOA
                        )

                    [1] => stdClass Object
                        (
                            [target] => ns1.test.com
                            [ref] => 
                            [host] => test.co.uk
                            [type] => NS
                        )

                    [2] => stdClass Object
                        (
                            [target] => ns2.test.com
                            [ref] => 
                            [host] => test.co.uk
                            [type] => NS
                        )

How can I turn each of the records ([0], [1], [2] etc) into variables? I am wanting to show them in a table

I have tried the below, but no result is shown

$Test=$PackageDNSInfo->test.co.uk->records[0]->mname; 
echo $Test;

Upvotes: 1

Views: 441

Answers (1)

Jeto
Jeto

Reputation: 14927

Just use foreach on it, just like you would on an array:

foreach ($PackageDNSInfo as $url => $data) {
    foreach ($data->records as $record) {
        $type = $record->type;
        $host = $record->host;
        // etc.
    }
}

If you want to access a given URL's data directly, use curly braces:

echo $PackageDNSInfo->{'test.co.uk'}->records[0]->mname;

Upvotes: 1

Related Questions