2012-10-29 224 views
0

我正在嘗試使用多維數組,我需要在foreach循環中使用每個字段,如下所示。可以有多個主機名,每個zoneId有redcordId,可以有多個zoneId。我目前能夠輸出主機名「example.com」但我無法使用第一和第二值和了zoneid ...的recordId多維PHP陣列

$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"); 
      } 
    } 

回答

6

$是了zoneid還是一個數組那裏,你缺少一個$ recordId中的「$」。您需要將其更改爲:

$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"); 
     } 
}