Foreach loop through array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MrRSMan
Forum Newbie
Posts: 20
Joined: Sun Feb 03, 2008 8:11 am

Foreach loop through array

Post by MrRSMan »

I have an array with the following structure:

$arr = array($nation => array($var1, $var2), $nation => array($var1, $var2), $nation => array($var1, $var2));

I want to loop through $nation and for each instance echo $var1 and $var2.

I've been trying dozens of foreach loops and nothing has worked.

I hope you can help- it'll be really appreciated.
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Foreach loop through array

Post by amargharat »

//suppose we have,

Code: Select all

$arr = array("uk" => array("a", "b"), "us" => array("c", "d"), "in" => array("e", "f"));

foreach($arr as $nation)
{
     echo $nation[0];
     echo " | ";
     echo $nation[1];
     echo "<br />";
}
use above code
MrRSMan
Forum Newbie
Posts: 20
Joined: Sun Feb 03, 2008 8:11 am

Re: Foreach loop through array

Post by MrRSMan »

Thanks Amargharat- massive help and much appreciated :)
Post Reply