moderith Posted June 18, 2008 Share Posted June 18, 2008 Hello World! I do hate going to a new forum and asking a question first thing, so I did help solve a problem first. Okay so lets say you have a multi-dimensional array, $row[1]["name"] = 'fred'; $row[1]["age"] = '25'; $row[1]["sex"] = 'male'; $row[2]["name"] = 'jack'; $row[2]["age"] = '42'; $row[2]["sex"] = 'male'; $row[3]["name"] = 'kerry'; $row[3]["age"] = '19'; $row[3]["sex"] = 'female'; and you want to spit out all that information in a loop. for($i=1;$i<=3;$i++){ echo 'Name: '.$row[$i]["name"].' | Age:'.$row[$i]["age"].' | Sex: '.$row[$i]["sex"].'<br />'; } Great, but wait a minute, there has to be a better way of doing this. I have seen it done with a mysql_fetch_assoc(). $result = mysql_query("SELECT * FROM users"); while($row = mysql_fetch_assoc($result)){ echo 'Name: '.$row["name"].' | Age:'.$row["age"].' | Sex: '.$row["sex"].'<br />'; } How did they do that? I mean there are multiple rows but I don't have to use $row[$i]["name"] I can just use $row["name"]. Is there a way I can turn my multi-dimensional array into something that I can execute with a while loop? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 18, 2008 Share Posted June 18, 2008 with arrays, use foreach() <?php $row[1]["name"] = 'fred'; $row[1]["age"] = '25'; $row[1]["sex"] = 'male'; $row[2]["name"] = 'jack'; $row[2]["age"] = '42'; $row[2]["sex"] = 'male'; $row[3]["name"] = 'kerry'; $row[3]["age"] = '19'; $row[3]["sex"] = 'female'; foreach ($row as $k => $person) { echo "<h3>Person $k</h3>"; foreach ($person as $attribute=>$value) { echo "$attribute : $value <br/>"; } } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 18, 2008 Share Posted June 18, 2008 Here's a similar solution: <?php $row[1]["name"] = 'fred'; $row[1]["age"] = '25'; $row[1]["sex"] = 'male'; $row[2]["name"] = 'jack'; $row[2]["age"] = '42'; $row[2]["sex"] = 'male'; $row[3]["name"] = 'kerry'; $row[3]["age"] = '19'; $row[3]["sex"] = 'female'; foreach ($row as $inx => $arr) { $tmp = array(); foreach ($arr as $fld => $val) $tmp[] = ucwords($fld) . ': ' . $val; echo implode(' | ', $tmp) . "<br>\n"; } ?> Ken (GMTA) Quote Link to comment Share on other sites More sharing options...
moderith Posted June 19, 2008 Author Share Posted June 19, 2008 Thanks for replying guys I appreciate it. I feel that more explanation is in order though. Your absolutely right I can do it that way and is how I have been doing it. Though I am still warping a foreach loop in a foreach loop, and my question still remains. I'm sorry but it is rather hard for me to put this question into words as it is kinda complicated in my own mind. So I will ask this instead. How is it that a mysql_fetch_assoc() works? why is it that I can have a 1000 rows of data from a query but still call them with the same array name ($row["name"]) in a while loop? $result = mysql_query("SELECT name FROM users"); if($row = mysql_fetch_assoc($result)){ echo $row["name"].'<br />'; } How do I take 1000 entries in an array and make them all spit out in a while loop by only using ($row["name"]) and not $row[1]["name"], $row[2]["name"], $row[3]["name"], $row[4]["name"] and so on? BTW how are you guys posting the code in a colored format that is cool I am putting the [ code ][ /code ] tags around it but don't get that. Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 While loops would check if that $row = mysql_fetch_assoc($result) is true. If it's true it will keep going through the rows. I'm guessing that when mysql_fetch_assoc ends all the rows it becomes false and ends the loop. For colored you have to do <?php at the beginning and ?> at the end. Quote Link to comment Share on other sites More sharing options...
moderith Posted June 19, 2008 Author Share Posted June 19, 2008 I understand that. How about this, is there a way I can see the coding behind a function like mysql_fetch_assoc? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 19, 2008 Share Posted June 19, 2008 Every time you call mysql_fetch_assoc, it will return one row from the result set. When there is no results left it will retrun false and the loop stops. The same functionality applies to a foreach loop. You could use a while loop too, to loop through an array except the syntax will be slightly different: while($row = each($rows)) { echo $row['name'] . ' - ' $row['age'] } Now as a foreach loop foreach($rows as $k => $row) { echo $row['name'] . ' - ' . $row['age']; } Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 19, 2008 Share Posted June 19, 2008 How about this, is there a way I can see the coding behind a function like mysql_fetch_assoc? AFAIK yes you can. But there is absolutely no reason to do so for what you're asking. In your example of mysql_fetch_assoc(), that is a function that you are passing a result set to. That function extracts a record from the result set. You have to use a special function (mysql_fetch_assoc) in this case because the argument ($result) is a resource. A resource is a built-in PHP data type, but PHP itself has no idea what the resource represents. Perhaps the resource represents a MySQL result set, or perhaps the resource represents an image from the GD library. In either case, PHP doesn't know what the resource represents. In those cases, you have to use a special function to get at the results (in your example, mysql_fetch_assoc()). An array on the other hand is a built in data type and PHP knows everything about how it's stored. PHP may not know what's in the array, but it does know enough about arrays to cycle through them. In this case (when using arrays), PHP has several built in methods of cycling through the elements. The most convenient of which is the foreach. I hope that explanation helps some. Quote Link to comment Share on other sites More sharing options...
moderith Posted June 19, 2008 Author Share Posted June 19, 2008 thanks everyone. Yah seems like foreach is the way to go. Quote Link to comment 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.