Page 1 of 1

Populating an array

Posted: Sun Jun 13, 2010 4:26 pm
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.

Re: Populating an array

Posted: Sun Jun 13, 2010 5:31 pm
by internet-solution
Try this

Code: Select all


$nationArray=array();
foreach($nations as $nation){
        print($var1);
        print($var2);
        $nationArray[$nations]=array($var1,$var2);
}
 

Re: Populating an array

Posted: Mon Jun 14, 2010 5:11 am
by MrRSMan
Thank you internet-solution- your code worked perfectly :)