2016-12-08 128 views
2

我有一個名爲$grouped_types的數組。當我打印出來它顯示是這樣的:多維數組讀取失敗

Array 
(
[XMU_1_1] => Array 
    (
     [0] => Array 
      (
       [port] => 1 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [1] => Array 
      (
       [port] => 2 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [2] => Array 
      (
       [port] => 3 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [3] => Array 
      (
       [port] => 4 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [4] => Array 
      (
       [port] => 5 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [5] => Array 
      (
       [port] => 6 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [6] => Array 
      (
       [port] => 7 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [7] => Array 
      (
       [port] => 8 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [8] => Array 
      (
       [port] => 9 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [9] => Array 
      (
       [port] => 10 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [10] => Array 
      (
       [port] => 11 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [11] => Array 
      (
       [port] => 12 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [12] => Array 
      (
       [port] => 13 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [13] => Array 
      (
       [port] => 14 
       [band] => 700 
       [name] => XMU_1_1 
      ) 

     [14] => Array 
      (
       [port] => 15 
       [band] => 
       [name] => XMU_1_1 
      ) 

     [15] => Array 
      (
       [port] => 16 
       [band] => 
       [name] => XMU_1_1 
      ) 
    ) 

[DUS_1_1] => Array 
    (
     [0] => Array 
      (
       [port] => A 
       [band] => 
       [name] => DUS_1_1 
      ) 

     [1] => Array 
      (
       [port] => B 
       [band] => 
       [name] => DUS_1_1 
      ) 

     [2] => Array 
      (
       [port] => C 
       [band] => 
       [name] => DUS_1_1 
      ) 

     [3] => Array 
      (
       [port] => D 
       [band] => 
       [name] => DUS_1_1 
      ) 

     [4] => Array 
      (
       [port] => E 
       [band] => 
       [name] => DUS_1_1 
      ) 

     [5] => Array 
      (
       [port] => F 
       [band] => 
       [name] => DUS_1_1 
      ) 
    ) 

[DUS_1_2] => Array 
    (
     [0] => Array 
      (
       [port] => A 
       [band] => 
       [name] => DUS_1_2 
      ) 

     [1] => Array 
      (
       [port] => B 
       [band] => 
       [name] => DUS_1_2 
      ) 

     [2] => Array 
      (
       [port] => C 
       [band] => 
       [name] => DUS_1_2 
      ) 

     [3] => Array 
      (
       [port] => D 
       [band] => 
       [name] => DUS_1_2 
      ) 

     [4] => Array 
      (
       [port] => E 
       [band] => 
       [name] => DUS_1_2 
      ) 

     [5] => Array 
      (
       [port] => F 
       [band] => 
       [name] => DUS_1_2 
      ) 
    ) 
) 

現在我想這個陣列在一個表格中顯示是這樣的:

XMU_1_1 port XMU_1_1 band 

1    
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14     700 
15 
16 

這是我想爲每一個不同的名字只有一個名字。每個名字都應該有不同的表格。如何取我的數組,以便我可以顯示它爲不同的表?我試過,但我不知道如何獲取這種類型的數組。

+4

_」我嘗試但「_ - 請顯示你已經嘗試過! – Jeff

回答

1
通過

的陣列,以循環的最簡單的方法是使用foreach() loop.With多維陣列,你必須具有一個或兩個嵌套foreach()循環,如代碼片段所示。我不是說這就是適合你的代碼;它只是說明一下嵌套的foreach()循環看起來like.Hope它可以幫助你解決你的問題

LIVE DEMO

<?php 

echo "<table>"; 
foreach ($grouped_types as $k => $val) { 
    echo "<tr> 
      <th>$k port </th> 
      <th>$k band </th> 
      </tr>"; 
    if (is_array($val)) { 
     foreach ($val as $v) { 
      echo "<tr> 
        <td>" . $v['port'] . "</td> 
        <td>" . $v['band'] . "</td> 
        </tr>"; 
     } 
    } 

} 
echo "</table>"; 

?> 

輸出:

enter image description here

0

根據您的陣列,這可以按照使用foreach()循環的方式進行。

$array = [ 

"XMU_1_1" =>[ 

      [ 

      "port" => 1, 
      "band" => null, 
      "name" => "XMU_1_1", 

      ], 
      [ 

      "port" => 2, 
      "band" => null, 
      "name" => "XMU_1_1", 

      ], 
      [ 

      "port" => 3, 
      "band" => null, 
      "name" => "XMU_1_1", 

      ], 
      [ 

      "port" => 4, 
      "band" => 700, 
      "name" => "XMU_1_1", 

      ],  
    ] 
]; 


echo "<table>"; 
echo "<tr><th>XMU_1_1 port</th><th>XMU_1_1 band</th></tr>"; 

foreach($array['XMU_1_1'] as $v) { 

    echo "<tr><td>" . $v['port'] . "</td><td>" . $v['band'] . "</td></tr>"; 

} 

echo "</table>"; 

輸出

XMU_1_1 port XMU_1_1 band 
1 
2 
3 
4 700