2014-03-03 92 views
0

我有一個看起來像這樣的數組。如何根據另一個數組的鍵和值打印出一個數組的鍵和值?

[DETROIT] => Array 
(
    [NORTH] => 20.00% 
    [SOUTH] => 30.00% 
    [WEST] => 25.00% 

) 

[CHICAGO] => Array 
(
    [NORTH] => 59.14% 
    [SOUTH] => 12.94% 
    [WEST] => 0.00% 
    [EAST] => 34.60% 
) 

[NEW YORK] => Array 
(
    [WEST] => 38.00% 
    [EAST] => 49.00% 
) 

[DALLAS] => Array 
(
    [WEST] => 60.57% 
) 

在另一個用戶的幫助下,我得到了代碼將它打印出來,就像這張表一樣。

  DETROIT  CHICAGO NEW YORK DALLAS 
NORTH  20.00  59.14  N/A  N/A 
SOUTH  30.00  12.94  N/A  N/A 
WEST  25.00  0.00  38.00  60.57 
EAST  N/A   34.60  49.00  N/A 

這是我爲下表打印出來代碼:

echo "<table>"; 
$heading = "<tr><td>&nbsp;</td>"; 
$stats_key = array("Stat 1","Stat 2","Stat 3"); 
$cities = array(); 
foreach ($stats as $city=>$city_stats){ 
$cities[] = $city; 
$heading .= "<td>" . $city . "</td>"; 
} 
$heading .= "</tr>"; 
foreach ($stats_key as $key){ 
$table .= "<tr><td>" . $key . "</td>"; 
foreach ($cities as $cit){ 
$table .= "<td>" . $stats[$cit][$key] . "</td>"; 
} 
$table .= "</tr>"; 
} 

echo $heading; 
echo $table; 

我有另一個數組,鍵是負責這些地區的人民。現在

[John Doe] => Array 
(
    [0] => DETROIT 
    [1] => DALLAS 

) 

[Sara Smith] => Array 
(

    [1] => NEW YORK 
) 

[Donald Duck] => Array 
(
    [0] => CHICAGO 
) 

,我只是想打印出來的同一個表,但是這是同一人下的位置扎堆打印出來,像這樣:

  DETROIT  DALLAS NEW YORK CHICAGO 
    NORTH  20.00  N/A  N/A  59.14 
    SOUTH  30.00  N/A  N/A  12.94 
    WEST  25.00  60.57 38.00  0.00 
    EAST  N/A   N/A  49.00  34.60 

任何幫助,將不勝感激,謝謝。

編輯: 這是我迄今所做的:

echo "<table>"; 
$heading = "<tr><td>&nbsp;</td>"; 
$stats_key = array("Stat 1","Stat 2","Stat 3"); 
$cities = array(); 

foreach($PERSON as $per){ 
foreach ($stats as $city=>$city_stats){ 
$cities[] = $city; 
$heading .= "<td>" . $city . "</td>"; 
} 
$heading .= "</tr>"; 
foreach ($stats_key as $key){ 
$table .= "<tr><td>" . $key . "</td>"; 
foreach ($cities as $cit){ 
$table .= "<td>" . $stats[$cit][$key] . "</td>"; 
} 
$table .= "</tr>"; 
} 

echo $heading; 
echo $table; 
} 
} 

但是,額外的foreach循環得到了我太多的重複。另外,沒有兩個人可以負責一個地方。

+0

所以你能證明你做了什麼來嘗試這些列聚集在一起? –

+0

不止一個人可以負責一個地方? – theB3RV

+0

什麼樣的重複? – theB3RV

回答

0

在你的代碼的最後的foreach

foreach ($cities as $cit){ 
    foreach($person_name as $charge){ 
     if($charge = $cit){ 
      $table .= "<td>" . $stats[$cit][$key] . "</td>"; 
      break; 
     }else{ 
      $table .= "<td> N/A </td>"; 
     } 
} 
0

據我瞭解,你需要爲它們出現在第二陣列中以相同的順序來顯示城市。

試試這個:

$array1 = array(
    'DETROIT' => array(
     'NORTH' => '20.00%', 
     'SOUTH' => '30.00%', 
     'WEST' => '25.00%', 
    ), 
    'CHICAGO' => array(
     'NORTH' => '59.14%', 
     'SOUTH' => '12.94%', 
     'WEST' => '0.00%', 
     'EAST' => '34.60%', 
    ), 
    'NEW YORK' => array(
     'WEST' => '38.00%', 
     'EAST' => '49.00%', 
    ), 
    'DALLAS' => array(
     'WEST' => '60.57%', 
    ), 
); 
$array2 = array(
    'John Doe' => array(
     'DETROIT', 
     'DALLAS', 
    ), 
    'Sara Smith' => array(
     'NEW YORK', 
    ), 
    'Donald Duck' => array(
     'CHICAGO', 
    ), 
); 

function get2ndLevel($arr, $bKeys = false) { 
    $vals = array(); 
    array_walk($arr, function($v, $k) use(&$vals, $bKeys){ 
     $vals = array_merge($vals, $bKeys? array_keys($v) : array_values($v)); 
    }); 
    return array_values(array_unique($vals)); 
} 

$dirs = get2ndLevel($array1, true); 
$cities = get2ndLevel($array2); 
$cities = array_merge($cities, 
    array_values(array_diff(array_keys($array1), $cities)) 
); 

$table = "<tr><td>&nbsp;</td>"; 
foreach ($cities as $city){ 
    $table .= "<td>" . $city . "</td>"; 
} 
$table .= "</tr>"; 
foreach ($dirs as $dir){ 
    $table .= "<tr><td>" . $dir . "</td>"; 
    foreach ($cities as $city){ 
     $table .= "<td>" . (isset($array1[$city][$dir])? $array1[$city][$dir] : "N/A") . "</td>"; 
    } 
    $table .= "</tr>"; 
} 

echo "<table>". $table. "</table>"; 

Demo

+0

這就是我想要做的,但我不確定是否遵循。 – user3266259

+0

您的原始代碼無法輸出給定的表格。它必須修復。 查看我的更新回答。 – hindmost

相關問題