Foreach循环不起作用 - 这是一个嵌套数组吗?

时间:2013-07-15 04:58:27

标签: php arrays foreach

我有一个我似乎无法从中检索信息的数组。这是一个嵌套数组吗?

print_r()结果:

Array (
    [0] => Array (
        [0] => Array (
            [title] => Hampton
            [day] => 1st and 3rd Monday
            [time] => 7:30pm
            [contact] => Jan Boyd
            [phone] => 0438 584 558
            [email] =>
        )
        [1] => Array (
            [title] => Frankston
            [day] => 1st and 3rd Wed
            [time] => 9:30am
            [contact] => Vaness Ogues-Canele
            [phone] => 0420 834 791
            [email] =>
        ) 
    )
) 

我用来尝试检索信息的代码:

foreach( $groups as $group ) { ?>
    <?php echo $group['title'] ?>
    <?php echo $group['day'] ?>
    <?php if ($group['time']) { ?>       
<?php }  ?>

3 个答案:

答案 0 :(得分:7)

你有没有试过进入第一个阵列?

foreach( $groups[0] as $group ) { ?>
    <?php echo $group['title'] ?>
    <?php echo $group['day'] ?>
    <?php if ($group['time']) { ?>       
<?php }  ?>

如果可能,您可以尝试两个foreach循环:

foreach( $groups as $tmp ) { ?>
    foreach ($tmp as $group) {
        <?php echo $group['title'] ?>
        <?php echo $group['day'] ?>
        <?php if ($group['time']) { ?> 
    <?php } ?>      
<?php }  ?>

答案 1 :(得分:1)

foreach( $groups as $group ) 
 foreach( $group as $groups_re )  
  { 
    echo $groups_re['title'];
    echo $groups_re['day'] 
    if ($groups_re['time']) 
     {        
      }
  }

答案 2 :(得分:1)

试试这个

foreach( $groups as $group ) { 
if(is_array($group)) // it check's array or not 
{
 echo $group['title']; 
 echo $group['day']; 
 echo $group['time']; 
}
else
    echo $group;}