MqTontoh Posted January 8, 2010 Share Posted January 8, 2010 <?php /* I want to insert elements dynamically from a query result. The code below is what I could come up with. Is the anyway I can do it better so that the keys of the array also increment? */ while($row=mysql_fetch_array($thumbs)){ $new_array=array(); $new_array[]=$row['thumb_id']; array_unique($new_array); print_r($new_array); // Print result Array ([0] =>27) Array ([0] =>27) Array ([0] =>27) Array ([0] =>27) Array ([0] =>27) Array ([0] =>27) /* Is there any way to make the Array keys increment like below? Any help will be very much welcomed. Thanks in advance. */ Array ([0] => 27 [1] => 28 [2] => 29 [3] => 30 [4] => 31 [5] => 33) ?> Link to comment https://forums.phpfreaks.com/topic/187773-add-array-elements-dynamically-from-a-query-result/ Share on other sites More sharing options...
premiso Posted January 8, 2010 Share Posted January 8, 2010 Move the $new_array=array(); out of the loop, as you erase the data each time it loops: $new_array=array(); while($row=mysql_fetch_array($thumbs)){ Should increment just fine. Link to comment https://forums.phpfreaks.com/topic/187773-add-array-elements-dynamically-from-a-query-result/#findComment-991385 Share on other sites More sharing options...
MqTontoh Posted January 9, 2010 Author Share Posted January 9, 2010 Many thanks Premiso. Link to comment https://forums.phpfreaks.com/topic/187773-add-array-elements-dynamically-from-a-query-result/#findComment-991416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.