medoix
medoix

Reputation: 1179

How to iterate a multidimensional array?

I am attempting to use a multidimensional array and i need to use each field in a foreach loop as below. There can be multiple hostname's, redcordId's to each zoneId and there can be multiple zoneId's. I am currently able to output the hostname "example.com" but am unable to use the first and second values for zoneid and recordid...

$zone_record = array( );
$zone_record["34467"]["442899"] = "example.com";
$zone_record["34467"]["442875"] = "www.example.com";

foreach ($zone_record as $zoneId) {
    foreach ($zoneId as $recordId => $hostname) {
        echo("zoneId: " . $zoneId . "\n");
        echo("recordId: " . recordId . "\n");
        echo("hostname: " . $hostname . "\n");
    }
}

Upvotes: 0

Views: 89

Answers (1)

CodePB
CodePB

Reputation: 1756

$zoneId is still an array there, and you are missing a "$" from $recordId. You would need to change it to:

$zone_record = array( );
$zone_record["34467"]["442899"] = "example.com";
$zone_record["34467"]["442875"] = "www.example.com";

foreach ($zone_record as $zoneId => $zoneArray) {
        foreach ($zoneArray as $recordId => $hostname) {
                echo("zoneId: ".$zoneId."\n");
                echo("recordId: ".$recordId."\n");
                echo("hostname: ".$hostname."\n");
        }
}

Upvotes: 6

Related Questions