Jump to content

Format Output from Array


partlyD

Recommended Posts

Hi -

 

I am using dns_get_record():

 

$result = dns_get_record("google.com");
echo '<pre>';
print_r ($result);
echo '</pre>';

 

 

This returns:

 

Array
(
    [0] => Array
        (
            [host] => google.com
            [type] => SOA
            [mname] => ns1.google.com
            [rname] => dns-admin.google.com
            [serial] => 1416940
            [refresh] => 7200
            [retry] => 1800
            [expire] => 1209600
            [minimum-ttl] => 300
            [class] => IN
            [ttl] => 86400
        )

    [1] => Array
        (
            [host] => google.com
            [type] => TXT
            [txt] => v=spf1 include:_netblocks.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all
            [entries] => Array
                (
                    [0] => v=spf1 include:_netblocks.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all
                )

            [class] => IN
            [ttl] => 3600
        )

    [2] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 400
            [target] => google.com.s9b2.psmtp.com
            [class] => IN
            [ttl] => 900
        )

    [3] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 100
            [target] => google.com.s9a1.psmtp.com
            [class] => IN
            [ttl] => 900
        )

    [4] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 200
            [target] => google.com.s9a2.psmtp.com
            [class] => IN
            [ttl] => 900
        )

    [5] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 300
            [target] => google.com.s9b1.psmtp.com
            [class] => IN
            [ttl] => 900
        )

 

 

I am new to php and would like to know what tools/commands I can use to go about manipulating and formatting the above output.  So for example, I can arrange the display of the output as:

 

google.com IN MX 100 google.com.s9a1.psmtp.com

google.com IN MX 200 google.com.s9a2.psmtp.com

google.com IN MX 300 google.com.s9b1.psmtp.com

 

etc...

 

Any help or information would be much appreciated.

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/203366-format-output-from-array/
Share on other sites

foreach()

 

Should be all you need.

 

Thank you for your reply.

 

When I use foreach:

 

<?php
$arr = dns_get_record("google.com");
reset($arr);
while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
}

foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}
?>

 

 

The output shows the value of each as simply "Array":

 

Key: 0; Value: Array
Key: 1; Value: Array
Key: 2; Value: Array
Key: 3; Value: Array
Key: 4; Value: Array
Key: 0; Value: Array

 

The output shows the value of each as simply "Array":

 

This is because you have arrays within an Array.

 

I'll use the code you posted to help you better understand this.

Array
(
    [0] => Array
        (
            [host] => google.com
            [type] => SOA
            [mname] => ns1.google.com
            [rname] => dns-admin.google.com
            [serial] => 1416940
            [refresh] => 7200
            [retry] => 1800
            [expire] => 1209600
            [minimum-ttl] => 300
            [class] => IN
            [ttl] => 86400
        )

    [1] => Array
        (
            [host] => google.com
            [type] => TXT
            [txt] => v=spf1 include:_netblocks.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all
            [entries] => Array
                (
                    [0] => v=spf1 include:_netblocks.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all
                )

            [class] => IN
            [ttl] => 3600
        )

    [2] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 400
            [target] => google.com.s9b2.psmtp.com
            [class] => IN
            [ttl] => 900
        )

    [3] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 100
            [target] => google.com.s9a1.psmtp.com
            [class] => IN
            [ttl] => 900
        )

    [4] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 200
            [target] => google.com.s9a2.psmtp.com
            [class] => IN
            [ttl] => 900
        )

    [5] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 300
            [target] => google.com.s9b1.psmtp.com
            [class] => IN
            [ttl] => 900
        )

 

So if $result is the name of your variable, and you want to print the SOA record, you would need to do something like this:

echo $result[0][type];

That's because that record is in an array named '0' and the values of the array are what is printed inside f that array.

So,

echo $result[5][target];

 

Would print out: google.com.s9b1.psmtp.com.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.