Populating an 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

Populating an array

Post by MrRSMan »

I have a foreach loop which simply echoes several variables. What I want to do now is populate an array with these variables. My code is as follows:

Code: Select all

foreach($nations as $nation){
	print($var1);
	print($var2);
}
I would like these vars arranged in a multidimensional array with the structure as follows:

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

How can I achieve this?

Thank you in advance.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Populating an array

Post by internet-solution »

Try this

Code: Select all


$nationArray=array();
foreach($nations as $nation){
        print($var1);
        print($var2);
        $nationArray[$nations]=array($var1,$var2);
}
 
MrRSMan
Forum Newbie
Posts: 20
Joined: Sun Feb 03, 2008 8:11 am

Re: Populating an array

Post by MrRSMan »

Thank you internet-solution- your code worked perfectly :)
Post Reply