pedrobcabral Posted March 24, 2007 Share Posted March 24, 2007 <?php $i = array(); while($row = mysql_fetch_array($result)){ $i[$x] = $row['id']; $x++; } echo "<pre>"; print_r($i); echo "</pre>"; ?> This outputs the folowing: Array ( [] => 3 [1] => 4 [2] => 9 ) Why the first it is not [ 0 ] ? Can't figure out the way to turn it that way. Thank you. Link to comment https://forums.phpfreaks.com/topic/44153-small-array-problem/ Share on other sites More sharing options...
kenrbnsn Posted March 24, 2007 Share Posted March 24, 2007 The variable $x is not set during the first iteration of the loop. A better way to write this would be: <?php $i = array(); while($row = mysql_fetch_array($result)) $i[] = $row['id']; ?> When you use "[]" with arrays, PHP assigns the value to the next numerical array index automatically. This will give you what you want. Ken Link to comment https://forums.phpfreaks.com/topic/44153-small-array-problem/#findComment-214386 Share on other sites More sharing options...
pedrobcabral Posted March 24, 2007 Author Share Posted March 24, 2007 soprry and thank you. Link to comment https://forums.phpfreaks.com/topic/44153-small-array-problem/#findComment-214391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.