简体   繁体   中英

Foreach loop not working - is this a nested array?

I have an array that I can't seem to retrieve the info from. Is this a nested array?

print_r() results:

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] =>
        ) 
    )
) 

The code I am using to try to retrieve the info:

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

Have you tried going into the first array with this?

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

If possible, you could try two foreach loops:

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

Try this

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;}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM