多维数组未正确循环

时间:2013-12-10 07:33:27

标签: php multidimensional-array

我一直试图在整个下午解决这个问题。首先,这是我正在使用的数组的缩写版本:

Array
(
[rotation] => Array
    (
        [2013-11-01] => Array
            (
                [0] => 21
                .....
            )

        [2013-10-01] => Array
            (
                [0] => 33
                .....
            )
    )

[mr] => Array
    (
        [2013-11-01] => Array
            (
                [0] => 21
                ........
        .....
......

我一直在使用这段代码:

foreach ($metric_sort as $metric => $months) {
    $html .= '<tr><td style="width:125px">' . $metric . '</td>';
    foreach ($months as $month => $uids) {
        $html .= '<td>';
        foreach ($uids as $uid) {
            $html .= $uid;
        }
        $html .= '</td>';
    }
    $html .= '</tr>';
}

问题在于它重复自身 - 每一行(第一列除外)看起来与上一行相同:

enter image description here

对于主foreach父循环的每次迭代,months数组都会重复。忽略表的可读性,这将在以后才能正常运行。

我检查了数组,看它是否重复。这不是问题所在。我在代码的其他区域使用嵌套数组没有任何问题。我无法弄清楚问题是什么。感谢

编辑:数组中最后一行数据的示例。抱歉,格式有点不同。这是xdebug中更精简的版本。看起来只有这些行正在打印而不是其他行?我现在完全糊涂了!

Array (
'hopkins' => 
array (size=5)
  '2013-11-01' => 
    array (size=21)
      0 => int 21
      1 => int 23
      2 => int 24
      3 => int 25
      4 => int 27
      5 => int 31
      6 => int 32
      7 => int 33
      8 => int 34
      9 => int 20
      10 => int 19
      11 => int 7
      12 => int 10
      13 => int 11
      14 => int 12
      15 => int 13
      16 => int 15
      17 => int 16
      18 => int 17
      19 => int 18
      20 => int 35
  '2013-10-01' => 
    array (size=10)
      0 => int 33
      1 => int 32
      2 => int 24
      3 => int 21
      4 => int 19
      5 => int 18
      6 => int 17
      7 => int 15
      8 => int 12
      9 => int 10
  '2013-09-01' => 
    array (size=8)
      0 => int 33
      1 => int 32
      2 => int 25
      3 => int 24
      4 => int 19
      5 => int 18
      6 => int 17
      7 => int 12
  '2013-08-01' => 
    array (size=9)
      0 => int 32
      1 => int 33
      2 => int 19
      3 => int 18
      4 => int 17
      5 => int 24
      6 => int 25
      7 => int 27
      8 => int 12
  '2013-07-01' => 
    array (size=4)
      0 => int 24
      1 => int 32
      2 => int 17
      3 => int 18

0 个答案:

没有答案