0

我的阵列包括:

Array ( 
      [**Anconia**] => Array ( [1335372330] => 7 [1335190784] => 7 [1334883979] => 7 [1334672871] => 7 [1334539093] => 7 [1334337930] => 7 [1334189033] => 7 [1334007668] => 7 [1333567097] => 7 [1332973327] => 7 [1332533799] => 7 [1332215655] => 7 [1331697329] => 7 [1331239651] => 7 [1331075294] => 7 [1330983800] => 7 [1330658201] => 7 [1330484662] => 7 [1330202828] => 7 [1330028992] => 7 ) 
      [**Angellica**] => Array ( [1335372330] => 9 [1335190784] => 9 [1334883979] => 10 [1334539093] => 10 [1334337930] => 10 [1334189033] => 10 [1334007668] => 10 [1331697329] => 9 [1331239651] => 9 [1331075294] => 9 [1330983800] => 9 [1330658201] => 10 [1330484662] => 10 [1330202828] => 8 [1330028992] => 9 )
)

TimeStamp    Tally Number
[1335372330] => 7

尝试提取此内容时,使用 foreach 使我感到困惑。想要打印一次 NAME(以粗体打印)并显示时间戳 = 多少。

我该怎么做呢?

提前致谢!

编辑:

我的尝试

foreach($results as $k => $v) { 
   $THEAD .= "<th scope='col'>".$k."</th>"; 
   $TBODY .= "<tr><th scope='row'>".$v."</th>"; 
   $TBODY2 .= "<td>".$results[$k]."</td>"; 
}
4

3 回答 3

2

通常,您可以像遍历单级数组一样遍历嵌套数组。例子:

$myArray = array(
    'first-level-first-key' => array(
        'second-level-first-key' => 'some value',
        'second-level-second-key' => 'another value'
    ),
    'first-level-second-key' => array(
        'seond-level-another-key' => 'yet another value'
    )
);

foreach($myArray as $first_level_key => $first_level_value) {
    echo 'Key: '. $first_level_key .'<br />';
    echo 'Values: ';
    foreach($first_level_value as $second_level_key => $second_level_value) {
        echo $second_level_value .', ';
    }
    echo '<br /><br />';
}

这将打印出:

Key: first-level-first-key
Values: some value, another value,

Key: first-level-second-key
Values: yet another value,

现在您知道如何遍历嵌套数组了。玩得开心!

编辑

多一点解释,作为对 tmyie 下面评论的回答:正如您所见,每个元素本身就是一个数组:

$myArray['first-level-first-key'] = array('second-level-first-key' => ... );

第一个 foreach 循环遍历所有“第一级”数组(在我的示例中,键为“第一级-....”的元素)。因此,在该循环中,变量$first_level_value包含一个数组。第二个 foreach 循环遍历第二级数组。如果您在第二级元素中保存了另一个数组,那么这种循环嵌套实际上是无穷无尽的。

将其与在您面前放置几个盒子进行比较。在第一个循环中,您说“打开我面前的每个盒子”。然后对于您打开的每个盒子,嵌套循环会说“打开我在那个盒子里找到的每个盒子”,依此类推。

虽然如果你写这个,你的应用程序设计很可能有一些非常错误的地方,但下面的例子是完全有效的:

foreach($myArray as $x) {
    foreach($x as $y) {
        foreach($y as $z) {
            foreach($z as $p) {
                // etc
            }
        }
    }
}
于 2012-04-26T10:49:19.553 回答
1

既然$v是你的另一个数组foreach,你需要做另一个foreach

foreach($results as $k => $v) {
   foreach($v as $timestamp => $tally) {
      // do whatever you want
   }
}

我没有给你一个完整的代码,而是希望把它作为一个练习留给你,但这就是 - 根据你的问题 - 提取多维数组

于 2012-04-26T10:48:00.507 回答
1

你可以试试这个

<?php 

echo '<table>';
echo '  <tr>';
echo '    <th>Time Stamp</th>';
echo '    <th>Tally </th>';
echo '  </tr>';

    foreach($myArray as $timeStampArray) 
    {
       foreach($timeStampArray as $timestamp => $tally) 
       {

          echo '  <tr>';
          echo '    <td>'.$timestamp .'</td>';
          echo '    <td>'.$tally.' </td>';
          echo '  </tr>';
       }
    }
echo '</table>';
?>
于 2012-04-26T10:57:44.773 回答