MikeUK Posted April 18, 2008 Share Posted April 18, 2008 I need some help please. All the tutorials I've seen tell you how to search an array using a loop, but how do you create an array from the following? for ($a = 0; $a <= 100; $a ++) { for ($b = 0; $b <= 50; $b ++) { //Put each value of $b and each value $a into an array $new_array[$a][$b]= $x // Doesn't work! } } What I'm trying to do is create an array of all the $a values and $b values and then get an average for each set. Any array I try to create inside the loop is storing only one value. The next iteration is overwriting it like it would a variable. Any ideas? I'd really appreciate any help. Thanks Mike Quote Link to comment Share on other sites More sharing options...
jonsjava Posted April 18, 2008 Share Posted April 18, 2008 Declare the array first <?php $new_array = array(); for ($a = 0; $a <= 100; $a ++) { for ($b = 0; $b <= 50; $b ++) { //Put each value of $b and each value $a into an array $new_array[$a][$b]= $x // Doesn't work! } } Quote Link to comment Share on other sites More sharing options...
MikeUK Posted April 18, 2008 Author Share Posted April 18, 2008 Done that it still doesn't work Quote Link to comment Share on other sites More sharing options...
sasa Posted April 18, 2008 Share Posted April 18, 2008 add ; after $x <?php $new_array = array(); for ($a = 0; $a <= 100; $a ++) { for ($b = 0; $b <= 50; $b ++) { //Put each value of $b and each value $a into an array $new_array[$a][$b]= $x; // Doesn't work! } } Quote Link to comment Share on other sites More sharing options...
morph07 Posted April 18, 2008 Share Posted April 18, 2008 <?php $new_array = array(); for ($a = 0; $a <= 100; $a ++) { for ($b = 0; $b <= 50; $b ++) { $new_array[$a]=$b; } } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted April 18, 2008 Share Posted April 18, 2008 oh my. if what you really want to do is "create an array of all the $a values and $b values and then get an average for each set": <?php $array_a = array(); $array_b = array(); for ($a = 0; $a <= 100; $a ++) { $array_a[] = $a; } for ($b = 0; $b <= 50; $b ++) { $array_b[] = $b; } echo "Average of array_a is ".array_sum($array_a)/count($array_a)."<BR>"; echo "Average of array_b is ".array_sum($array_b)/count($array_b)."<BR>"; ?> Quote Link to comment Share on other sites More sharing options...
MikeUK Posted April 18, 2008 Author Share Posted April 18, 2008 Thanks to you all for taking the time to reply I did manage to do it after reading through a library book and did it in pretty much the same way as the last example. Thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.