2

I have the array below and I can't seem to figure out how to pull out the information from the array Team_1 and echo out the 0 - 5 values inside it.

 array (size=3)
      'Team_1' => 
        array (size=5)
          0 => string '1199' (length=4)
          1 => string '1182' (length=4)
          2 => string '1105' (length=4)
          3 => string '1212' (length=4)
          4 => string '891' (length=3)
      'Team_2' => 
        array (size=5)
          0 => string '' (length=0)
          1 => string '' (length=0)
          2 => string '' (length=0)
          3 => string '' (length=0)
          4 => string '' (length=0)
      'Team_3' => 
        array (size=5)
          0 => string '' (length=0)
          1 => string '' (length=0)
          2 => string '' (length=0)
          3 => string '' (length=0)
          4 => string '' (length=0)
2
  • what do you mean by pull out?? echo out?? delete it?? Commented Nov 17, 2015 at 19:05
  • Sorry Andrew, Yes Echo them out. Commented Nov 17, 2015 at 19:06

2 Answers 2

4

The key is Team_1 so you can reference it directly and do a foreach loop to do the echo (you can add any html you want to echo to format the values).

foreach ($arr['Team_1'] as $val) {
    echo $val.'<br>';
}

If your intention is to loop through all teams and echo values

foreach ($teams as $team => $vals) {
    echo $team;
    foreach ($vals as $val) {
        echo $val;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Holy heck. That was a LOT easier than I thought. Thank you so much man. Will tick as soon as post has been up for allowed time. Thanks again
Is there a way to know how many teams will be created. So instead of Team_1 do Team_$counter ?
You can use count($arr) to get the number.
1
$output1 = $array['Team_1'][0]; // Should output 1199
echo $output1;


OR

foreach ($array['Team_1'] as $data){
 echo $data.'<br/>';
}

1 Comment

Thanks SamyQc! Was just what I was after. Do you know how instead of doing ['Team_1'] I can do ['Team_'.$counter] ?

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.