Jump to content

Recommended Posts

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";
?>
 

 

Link to comment
https://forums.phpfreaks.com/topic/276599-display-results-thru-looping-in-a-array/
Share on other sites

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.