MetalSmith Posted March 6, 2011 Share Posted March 6, 2011 Man this is really driving me nuts. I can see my array values with echo inside the loop but outside nothing is shown. What am I missing here? If anything it should show the last value in the loop. while ($x = mysql_fetch_array($query_name,MYSQL_NUM)) { echo $x[0]; } echo $x[0]; Link to comment https://forums.phpfreaks.com/topic/229736-array-and-loops/ Share on other sites More sharing options...
MetalSmith Posted March 6, 2011 Author Share Posted March 6, 2011 This works seems like. $a = array(); while ($x = mysql_fetch_array($query_name,MYSQL_NUM)) { $a[] = $x[0]; } echo ($a[0]); Link to comment https://forums.phpfreaks.com/topic/229736-array-and-loops/#findComment-1183441 Share on other sites More sharing options...
jcbones Posted March 6, 2011 Share Posted March 6, 2011 After the while loop, $x = false; So, it will not echo anything. while(this.condition.equals.true) Link to comment https://forums.phpfreaks.com/topic/229736-array-and-loops/#findComment-1183443 Share on other sites More sharing options...
MetalSmith Posted March 6, 2011 Author Share Posted March 6, 2011 Thanks for the help Jcbones. Sorry for the noob questions but I have one more How come the this piece of code automatically fills the $a array? What is it about the [] on the $a & $x that makes it fill up? $a = array(); while ($x = mysql_fetch_array($query_name,MYSQL_NUM)) { $a[] = $x[0]; } echo ($a[0]); Link to comment https://forums.phpfreaks.com/topic/229736-array-and-loops/#findComment-1183546 Share on other sites More sharing options...
jcbones Posted March 6, 2011 Share Posted March 6, 2011 I'm not real good at explanations, but I'll try to be clear. $a = array(); //define $a as an empty array. while ($x = mysql_fetch_array($query_name,MYSQL_NUM)) //mysql_fetch_array will return false after the resource handle has run out, so when the query is empty, $x = false which breaks the while loop. { $a[] = $x[0]; //if $x is passed to the while loop, it will contain an array full of row data. Currently you are asking it to only pass the first value to the $a array. //You then append this data to the $a array ([] denotes an array.) } echo ($a[0]); //you only echo the first value from the $a array. Arrays explained better than I ever could Link to comment https://forums.phpfreaks.com/topic/229736-array-and-loops/#findComment-1183662 Share on other sites More sharing options...
MetalSmith Posted March 6, 2011 Author Share Posted March 6, 2011 I think I understand. Thanks Man! Link to comment https://forums.phpfreaks.com/topic/229736-array-and-loops/#findComment-1183703 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.