phoenicoperu Posted August 3, 2011 Share Posted August 3, 2011 This is what i got: $marray = array(); for ($i=0;$i<3;$i++) { for ($j=3;$j>0;$j--) { $marray[] = array( $i => array ($j) ); } } echo "<pre>"; print_r ($marray); echo "</pre>"; OUTPUT: Array ( [0] => Array ( [0] => Array ( [0] => 3 ) ) [1] => Array ( [0] => Array ( [0] => 2 ) ) [2] => Array ( [0] => Array ( [0] => 1 ) ) [3] => Array ( [1] => Array ( [0] => 3 ) ) [4] => Array ( [1] => Array ( [0] => 2 ) ) [5] => Array ( [1] => Array ( [0] => 1 ) ) [6] => Array ( [2] => Array ( [0] => 3 ) ) [7] => Array ( [2] => Array ( [0] => 2 ) ) [8] => Array ( [2] => Array ( [0] => 1 ) ) ) But i would like to have as output something like: Array ( [0] => Array ( [1] => 3 [2] => 2 [3] => 1 ) [1] => Array ( [1] => 3 [2] => 2 [3] => 1 ) .........................etc ) How can i accomplish this using for or while loops? Thanks in advance. Use CODE tags next time. Quote Link to comment https://forums.phpfreaks.com/topic/243721-help-with-multidimensional-arrays/ Share on other sites More sharing options...
AyKay47 Posted August 3, 2011 Share Posted August 3, 2011 foreachloop Quote Link to comment https://forums.phpfreaks.com/topic/243721-help-with-multidimensional-arrays/#findComment-1251376 Share on other sites More sharing options...
AbraCadaver Posted August 3, 2011 Share Posted August 3, 2011 Two ways for a quick fix: $marray = array(); for ($i=0;$i<3;$i++) { $marray[$i] = array(); for ($j=3;$j>0;$j--) { $marray[$i] = array_merge($marray[$i], array($i => $j)); } } $marray = array(); for ($i=0;$i<3;$i++) { $marray[$i] = array(); for ($j=3;$j>0;$j--) { array_push($marray[$i], $j); } } Or something easier: foreach(range(0, 2) as $i) { $marray[$i] = range(3, 1); } Quote Link to comment https://forums.phpfreaks.com/topic/243721-help-with-multidimensional-arrays/#findComment-1251396 Share on other sites More sharing options...
Zane Posted August 3, 2011 Share Posted August 3, 2011 This seems like the quickest way to me. $marray = array(); for ($i=0;$i $marray[$i] = array(); for ($j=3;$j>0;$j--) $marray[$i][] = $j; } Quote Link to comment https://forums.phpfreaks.com/topic/243721-help-with-multidimensional-arrays/#findComment-1251399 Share on other sites More sharing options...
AbraCadaver Posted August 3, 2011 Share Posted August 3, 2011 This seems like the quickest way to me. $marray = array(); for ($i=0;$i<3;$i++) { $marray[$i] = array(); for ($j=3;$j>0;$j--) $marray[$i][] = $j; } Yes. Same as the array_push() but without the function call. I still prefer: foreach(range(0, 2) as $i) { $marray[$i] = range(3, 1); } Or: for($i=0; $i<3; $i++) { $marray[] = range(3, 1); } Or other variations using $i or not. If I can think of a way to get rid of the first loop I will post it Quote Link to comment https://forums.phpfreaks.com/topic/243721-help-with-multidimensional-arrays/#findComment-1251402 Share on other sites More sharing options...
AbraCadaver Posted August 3, 2011 Share Posted August 3, 2011 Ha, OK got it (may not be relevant, but oh well): $marray = array_fill(0, 3, range(3, 1)); Quote Link to comment https://forums.phpfreaks.com/topic/243721-help-with-multidimensional-arrays/#findComment-1251403 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.