1

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;
4
  • 3
    Use curly braces: $PackageDNSInfo->{'test.co.uk'}->records[0]->mname; Commented Oct 4, 2020 at 16:25
  • @Jeto - thanks, is there a way to do a 'foreach' to display them all in a table? Commented Oct 4, 2020 at 16:26
  • Yes, you can foreach on a stdClass object if you'd like (key will be the property name, value will be its value). Commented Oct 4, 2020 at 16:30
  • @Jeto - Are you able to provide an example as an answer? Commented Oct 4, 2020 at 16:35

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but I am wanting a foreach for each of the records, so i can display them in a table. something like $Type=$PackageDNSInfo->{"$RelDomIDName"}->records[5]->type; $Host=$PackageDNSInfo->{"$RelDomIDName"}->records[5]->host; $Ref=$PackageDNSInfo->{"$RelDomIDName"}->records[5]->ref; $IP=$PackageDNSInfo->{"$RelDomIDName"}->records[5]->ip;
Okay so doing this, seems to show nothing when i echo.... foreach ($PackageDNSInfo as $url => $data) { foreach ($data['records'] as $record) { $type = $record->type; $host = $record->host; echo"$type"; }}
Had a typo in there ($data['records'] should've been $data->records), should be better now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.