lhynx31 Posted April 6, 2013 Share Posted April 6, 2013 Hi. I need help on how should I print the results using a foreach loop or for loop in a multi-dimensional array. The output should be something like this: ---------------------------------- CustomerNumber CustomerName - Phone - Address ---------------------------------- and if there is a two CustomerName but a same CustomerName, output should be like this: ---------------------------------- CustomerNumber CustomerName - Phone - Address CustomerName - Phone - Address ---------------------------------- Please need help..Thanks... Here is sample of my code: <?php include 'mysql_connect.inc'; echo "</br></br>"; $query = "SELECT * FROM customers"; $result = mysqli_query($conn, $query); while($row = mysqli_fetch_array($result)) { $header = $row["customerNumber"]; $details = $row["customerName"]."^".$row["phone"]."^".$row["addressLine1"]; $array[$header][$details] = "header & details"; } foreach($array as $arr1){ $key = key($arr1); echo $key."</br>"; } echo "<hr></br>test by: Lloyd Liongson"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/276599-display-results-thru-looping-in-a-array/ Share on other sites More sharing options...
denno020 Posted April 6, 2013 Share Posted April 6, 2013 I have a feeling that your code won't actually put multiple $details for a single $header as you might hope it would.. The way you should probably do it, if there is going to be multiple $details for a single $header, is like this: while(...){ $header = ...; $details = ...; $array[$header][] = $details; } The empty square brackets after header will auto increase everytime a $details is added to a $header that already exists. So for example, $array["header1"][1] will access the second lot of $details that is associated with 'header1'. Then to loop through that, simply use a foreach loop: foreach($array as $arrHeader => $arrDetailsArray){ echo $arrHeader; foreach($arrDetailsArray as $arrDetails){ echo $arrDetails; } } That should get you something close to what you want . Denno Quote Link to comment https://forums.phpfreaks.com/topic/276599-display-results-thru-looping-in-a-array/#findComment-1423252 Share on other sites More sharing options...
lhynx31 Posted April 6, 2013 Author Share Posted April 6, 2013 Hi Sir. May I ask what does this symbol " => " means for? i thought this was only use for a class calling its function.. Sorry sir, I'm still in learning of php. Quote Link to comment https://forums.phpfreaks.com/topic/276599-display-results-thru-looping-in-a-array/#findComment-1423280 Share on other sites More sharing options...
lhynx31 Posted April 6, 2013 Author Share Posted April 6, 2013 (edited) .I forgot to say thanks, the code works! Thank you for your help. Edited April 6, 2013 by lhynx31 Quote Link to comment https://forums.phpfreaks.com/topic/276599-display-results-thru-looping-in-a-array/#findComment-1423282 Share on other sites More sharing options...
denno020 Posted April 6, 2013 Share Posted April 6, 2013 In the case that I've used the '=>' in the foreach loop, what that does is assign the key of the array to the first variable on the left of the => sign, and then the value to the variable on the right hand side. This is especially helpful for key value array pairs, where the key isn't just an incremented number. For class/objects, the notation is '->', Denno Quote Link to comment https://forums.phpfreaks.com/topic/276599-display-results-thru-looping-in-a-array/#findComment-1423345 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.