Hi,

I am chasing after a html table which will be made based on a multidimensional array.

The structure is

Array
(
    [az] => Array
        (
            [0] => Array
                (
                    [work] => dsdsds
                    [time] => 2:47---2:55
                    [total] => 8
                )

        )

    [an] => Array
        (
            [0] => Array
                (
                    [work] => sdsdsdsdsd
                    [time] => 1:47---2:47
                    [total] => 60
                )

        )

    [mu] => Array
        (
            [0] => Array
                (
                    [work] => sdsdsd
                    [time] => 1:30---2:48
                    [total] => 78
                )

        )

    [raj] => Array
        (
            [0] => Array
                (
                    [work] => dsdwew
                    [time] => 3:34---3:40
                    [total] => 6
                )

            [1] => Array
                (
                    [work] => cdsfdfdfd
                    [time] => 3:25---3:35
                    [total] => 10
                )

        )

)

The problem is I want to show the usernames as header and left as row something like this

User1         User2          User3      User4
aaa            dfdfd            fdfds        fdsfdsf

Please suggest me the best possible way....

Thanks,
Raj

Maybe something like this:

<?php
$table = "<table>";
foreach($array as $value) {
    foreach($value as $initials) {
    $table .= "<tr>";
        foreach($intials as $info) {
            $table .= "<td>$info</td>";
        }
        $table .= "</tr>";
    }
}
$table .= "</table>";
?>

Not sure exactly how you want the layout to look, but that's basically what I'd do.

Maybe something like this:

<?php
$table = "<table>";
foreach($array as $value) {
    foreach($value as $initials) {
    $table .= "<tr>";
        foreach($intials as $info) {
            $table .= "<td>$info</td>";
        }
        $table .= "</tr>";
    }
}
$table .= "</table>";
?>

Not sure exactly how you want the layout to look, but that's basically what I'd do.

Hi

Many thanks for your suggestion....I have tried with it a lot but nothing is getting displayed.

I would like to display in this format

User1               User2             User3          User4
  
      aaa                  dfdfd              fdfds            fdsfdsf

I can't understand the looping...plz help

The easiest way to cycle through an array is with the foreach() function. You can read about the syntax here.
With a multi-dimensional array, you'd have to have a one or two nested foreach() loops as demonstrated in the code snippet I posted before.
I'm not saying that's exactly the code that will work for you; it's just to illustrate what nested foreach() loops would look like.

This is the example they give in the manual:

/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

The easiest way to cycle through an array is with the foreach() function. You can read about the syntax here.
With a multi-dimensional array, you'd have to have a one or two nested foreach() loops as demonstrated in the code snippet I posted before.
I'm not saying that's exactly the code that will work for you; it's just to illustrate what nested foreach() loops would look like.

This is the example they give in the manual:

/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

yeah basically this is the very basic and good for starting.Now i have modified the code a bit

<?
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[user1][activity] = "a";
$a[user2][activity] = "b";
$a[user3][activity] = "y";
$a[user4][activity] = "z";

echo '<pre>';
print_r($a);
echo '</pre>';

foreach ($a as $k1=>$v1) {
    foreach ($v1 as $v2) {
        echo "$k1\n";
		echo "$v2\n";
    }
}
?>

If you see the output it comes as

user1 a user2 b user3 y user4 z

But my problem is that I want it to display as

user1        user2         user3            user4   
a               b                y                    z

How can I achieve this and what else I have to add....i assume that a /n or <br> is needed.....but i need to understand the logic

Do you fully understand how to create a table in HTML?
It's basiclally like this:

<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>

In the above PHP example, the "\n" newlines are sort of useless. This only applies when using premormatted text or in a plain text file. It doesn't change what the user sees, otherwise.
Try something like this:

<?
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[user1][activity] = "a";
$a[user2][activity] = "b";
$a[user3][activity] = "y";
$a[user4][activity] = "z";

echo '<pre>';
print_r($a);
echo '</pre>';

echo '<table border="1">';
foreach ($a as $k1=>$v1) {
    foreach ($v1 as $v2) {
        echo "<tr><td>$k1</td>";
		echo "<td>$v2</td></tr>\n";
    }
echo '</table>';
}
?>

According to pure html logic this is correct....but isnt there no way to make the rows as columns....like

<table border="1">
<tr>
<td>user1</td>
<td>user2</td>
<td>user3</td>
<td>user4</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>y</td>
<td>z</td>
</tr>
</table>

That's exactly what Jerail gave you!
Why don't you experiment with this stuff before posting?
Also there are special tags used for header titles in html.

<table border="1">
<thead>
<tr>
<th>user1</th>
<th>user2</th>
<th>user3</th>
<th>user4</th>
</tr>
</thead>
<tr>
<td>a</td>
<td>b</td>
<td>y</td>
<td>z</td>
</tr>
</table>

The thead defines a table header row for the data below.
the th is the same as the the td , but emphasized bold print and centered.
to make things look better use the attributes appropriate for the tags to justify the text where you want it and create borders, etc.

Take a look at the w3 tutorials. They even provide "sandboxes" for experimentation.
You are not going to learn anything by posting questions about every little detail.
At this point you don't even understand what has been imparted to you already, because you haven't run the code (I guess.)
This is a re-hash of a previous post on the exact same subject that got apparently you nowhere.
Just do it!

Hi JRM,

I have experimented with the code a lot and i have given my opinion based on the test i have made.Also that site w3 schools doesn't explain enough info regarding multidimensional array.

May be you have not understood my question well enough or may be i have not clearly explained what I need to know.

Anyway...these are 2 examples below which will show you how the normal tr td moves with foreach loop in html according to rows- columns and columns- rows.

$Rockbands = array(
 array('Rockband', 'Song 1', 'Song 2', 'Song 3'),
 array('Beatles','Love Me Do', 'Hey Jude','Helter Skelter'),
 array('Rolling Stones','Waiting on a Friend','Angie','Yesterday\'s Papers'),
 array('Eagles','Life in the Fast Lane','Hotel California','Best of My Love')
);
echo '<pre>';
print_r($Rockbands);
echo '</pre>';
?>
<table border="1">
<?php
foreach($Rockbands as $Rockband)
{
 echo '<tr>';
 foreach($Rockband as $item)
 {
  echo "<td>$item</td>";
 }
 echo '</tr>';
}
?>
</table>
<hr />
<?php
$Rockbands1= array();
foreach ($Rockbands as $i => $Rockband){
   foreach ($Rockband as $j => $value) $Rockbands1[$j][$i] = $value;
}
echo '<table border="1">';
foreach($Rockbands1 as $key => $Rockband)
{
 $tag = ($key === 0) ? 'th' : 'td';
 echo '<tr>';
 foreach($Rockband as $item)
 {
  echo "<$tag>$item</$tag>";
 }
 echo '</tr>';
}
?>

I asked about the second table...not the first one....hope its cleared now...

Thanks,
Raj

I invite you to re-read your last post.
There is no way ANYONE could read that post and understand what you are asking.
You never got to the point, just some vague reference to the "second table".
Besides, the code you have there has problems. You were already told not to use print_r, but it's still there. The second table echo statement will not echo the variables.
do it like this:

echo "<".$tag.">".$item."</".$tag.">";

The second table echo statement will not echo the variables.
do it like this:

echo "<".$tag.">".$item."</".$tag.">";

it seems that you have not run the code...yes that echo statement in the second table will echo the values inside the variables.

What is this supposed to accomplish?

foreach ($Rockbands as $i => $Rockband){
   foreach ($Rockband as $j => $value) $Rockbands1[$j][$i] = $value;
}

It serves absolutely no purpose as far as I can tell. Perhaps you meant to do foreach($Rockband as $j => &$value) , but even then it's rather vague what you're trying to accomplish.


Perhaps you should post a summary of what exactly your aim is. That way we can suggest better methods for you.


And with regards to the last post about the quotes. I would suggest that whoever said "<$tag>" wouldn't work reads about strings. Double-quoted (semi-literal) strings don't require concatenation to insert variables.

Why can't you go through both of the output/table generated from the above code and view the difference between each table.Obviously that piece of code helps to generate the second table...

For example from the above code first table looks like

User1          aa           ee
User2          bb           ff
User3          cc            gg 
User4          dd           hh

second table looks like

User1          User2           User3      User4
aa                bb                cc            dd
ee                ff                  gg           hh

Hope you can view the difference now....

I have attached a file which can describe better than words.I want to do this type of report.

In that file Monday tuesday wednsday.....will become user1,user2....so on.The boxes will be the activity done by the user on a particular date and within a certain hour.


And with regards to the last post about the quotes. I would suggest that whoever said "<$tag>" wouldn't work reads about strings. Double-quoted (semi-literal) strings don't require concatenation to insert variables.

Fair enough! I have also found that enclosing more complex variables like arrays in an echo string works better (as opposed to not working) if they are enclosed in curly braces.
I believe that causes PHP to parse the array first, then drops in the result?
Non-array vars will work OK either way, however.

Yes, when you want to place an array inside a string, you either have to concatenate or use curly braces. This is because, in a string, after the $, PHP only parses trailing alphanumeric characters as part of the string. Anything else is just text (i.e. a space or square bracket).

The same thing applies when you have a variable right next to a part of the string with no separating space. Consider the following example:

<?php
$word = "Hello "; // note the trailing space;
echo "$wordWorld"; // outputs "" ($wordWorld is undefined)
echo "{$word}World"; // outputs "Hello World"
echo "${word}World"; // outputs "Hello World" (dollar sign may appear either inside or outside the curly braces).
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.