0

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 }  ?>
1
  • Yes, this is a nested array! You can loop through it by either using Dave Chen's answer or nesting multiple foreach-loops. Commented Jul 15, 2013 at 5:03

3 Answers 3

7

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 }  ?>
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.