niceduncan Posted March 31, 2010 Share Posted March 31, 2010 In the following code to retrive data from MySQL: while ($menu_row = mysqli_fetch_array($result, MYSQLI_NUM)) { echo "<option value=\"$menu_row[0]\">$menu_row[1]</option>\n"; } I want to store sets of $menu_row[0] => $menu_row[1] in a new array for later use. How do I achieve that? For example: while(){ $new_array[] = $menu_row[0] => $menu_row[1]; } but it wont work!!! Thanks a lot Link to comment https://forums.phpfreaks.com/topic/197118-a-simple-question-how-do-i-store-them-in-array-during-loop/ Share on other sites More sharing options...
MatthewJ Posted March 31, 2010 Share Posted March 31, 2010 while(){ $new_array[$menu_row[0]] = $menu_row[1]; } That would do what it looks like you're trying to accomplish. Link to comment https://forums.phpfreaks.com/topic/197118-a-simple-question-how-do-i-store-them-in-array-during-loop/#findComment-1034701 Share on other sites More sharing options...
ChemicalBliss Posted March 31, 2010 Share Posted March 31, 2010 A few things to note about arrays: 1. You can dynamically extend array sizes, eg: <?php $array = array(); while($row = mysql_fetch_assoc($resultset)){ $array[] = $row[0]; } ?> 2. Arrays can be dynamically called: <?php $array = array("number1","number2","number3"); for($i=0;$i<count($array);$i++){ echo($array[$i]); echo("<br>"); } ?> 3. Arrays can be as many dimensions as needed: <?php $array = array( array( array( array( array( "1" => array( array( "a") ) ), array( "2" => "b" ) ) ) ) ); ?> No. 2 is probably what your after. -CB- Link to comment https://forums.phpfreaks.com/topic/197118-a-simple-question-how-do-i-store-them-in-array-during-loop/#findComment-1034714 Share on other sites More sharing options...
niceduncan Posted March 31, 2010 Author Share Posted March 31, 2010 while(){ $new_array[$menu_row[0]] = $menu_row[1]; } That would do what it looks like you're trying to accomplish. WOW~~ Thanks a lot MJ!!! ^_~ Link to comment https://forums.phpfreaks.com/topic/197118-a-simple-question-how-do-i-store-them-in-array-during-loop/#findComment-1034741 Share on other sites More sharing options...
niceduncan Posted March 31, 2010 Author Share Posted March 31, 2010 A few things to note about arrays: 1. You can dynamically extend array sizes, eg: <?php $array = array(); while($row = mysql_fetch_assoc($resultset)){ $array[] = $row[0]; } ?> 2. Arrays can be dynamically called: <?php $array = array("number1","number2","number3"); for($i=0;$i<count($array);$i++){ echo($array[$i]); echo("<br>"); } ?> 3. Arrays can be as many dimensions as needed: <?php $array = array( array( array( array( array( "1" => array( array( "a") ) ), array( "2" => "b" ) ) ) ) ); ?> No. 2 is probably what your after. -CB- Thanks for trying to help me. No.2 is not the solution but all three of them gives me the hint Link to comment https://forums.phpfreaks.com/topic/197118-a-simple-question-how-do-i-store-them-in-array-during-loop/#findComment-1034742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.