Jump to content

Multidimensional Array into an Html table, help!


Ageneric

Recommended Posts

Hello all, I am new to HTML/PHP so any help would be great.

 

I have a multidimensional array filled with values that I am trying to output into an html table. 

 

This works (outputs value of array):

echo $table[0][0];

 

This does not work (outputs "Array[0]" instead of the actual value within the table):

echo "<table border = 1>\n";
echo "<td>Col 1</td><td>Col 2</td><td>Col 3</td>";
echo "</tr>\n";
echo "<td>$table[0][0]</td><td>$table[0][0]</td><td>$table[0][0]</td>";
echo "</table>\n";


I was going to use a couple of loops to output the array into the table however the way I have above is not working and really screwing me up.  Does anyone have a solution?  Thanks

Link to comment
Share on other sites

Something like:

 

$j = count($array);

$i=1;

echo '<table><tr>';

for($i;$i<=$j'$i++)

{

echo "<td>Col $i</td>";

}

echo "</tr><tr>";

foreach ($array as $sub)

{

$i=1;

foreach($sub as $item)

{

echo "<td>$item</td>";

if ($i%$j==0){echo "</tr><tr>";}

}

$i++;

}

 

 

HTH

Teamatomic

 

Link to comment
Share on other sites

Personally, I perfer to include my variables within the quoted string. The problem you were having is caused by how PHP interprets the multidimensional array in the string. You can overcome that by enclosing the variable within curly braces. I typically always enclose my variables in that manner.

 

Example:

echo "<table border=\"1\">\n";
echo "<tr>\n";
echo "<td>Col 1</td><td>Col 2</td><td>Col 3</td>";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>{$table[0][0]}</td><td>{$table[0][0]}</td><td>{$table[0][0]}</td>";
echo "</tr>\n";
echo "</table>\n";

Link to comment
Share on other sites

Personally, I perfer to include my variables within the quoted string. The problem you were having is caused by how PHP interprets the multidimensional array in the string. You can overcome that by enclosing the variable within curly braces. I typically always enclose my variables in that manner.

Thanks, I figure once I get more practice with html/php I will learn these little "tricks" that help me organize and make my code more neat.

 

One more quick question.  In PHP, is there any way to swap the contents in a multidimensional array?  For example,  I want all of the values in Array[$i][1] to be swapped with the values in Array[$i][2].  This is because I am trying to output the values in a certain order and the way they are ordered in the array is not how I want to output them (therefore when I output them in a loop it is in the wrong order).  Thanks

Link to comment
Share on other sites

The only use one loop and do something like:

echo "<td>{$sub[3]}</td><td>{$sub[0]}</td><td>{$sub[2]}</td><td>{$sub[1]}</td>";

 

 

HTH

Teamatomic

 

Im not so sure I know exactly what you mean.  Let me provide some of my code ($counter is a variable that holds a value and increments so you can ignore it for the purposes of this question).

for($i = 0; $i < 5; $i++)
{
for($j = 0; $j <= 2; $j++)
{
echo "<td>".$table[$counter][$j]."</td>";
}

 

That is how the loops are structured. I would like the outputs when $j = 1 and $j = 2 to be switched?  If there isnt an easy way than dont worry about it.

 

By the way, it seems you're a skier, where do you ski at? :)

 

 

Link to comment
Share on other sites

You can create a custom function to order the array however you want using usort().

 

So, let's say you want to order the array by items at the index 2 in ascending order, and if those items are equal, then do a sub-sort on the items at index 4 in descending order:

 

function customSort($a, $b)
{
    if ($a[2] == $b[2])
    {
        if ($a[4] == $b[4]) { return 0; }
        return ($a[4] < $b[4]) ? 1 : -1;
    }
    return ($a[2] > $b[2]) ? 1 : -1;
}

usort($myArray, 'customSort');

Link to comment
Share on other sites

:confused: Why not just use PHP's array_replace() function?

 

Interesting, would that work for a multidimensional array?

 

How is array_replace() going to help you? That function is meant to "replace" values in one array with the values from another array. You have a single array that you want to output into an HTML table. All you need to do is perform a foreach() on the array and output the results accordingly. If you want to reorder a multi-dimensional array then use usort() as I stated.

 

If you want more specific help then do a print_r() on your array and copy the results to a txt file and attach to this thread. Then provide what columns you want displayed and how they should be sorted.

Link to comment
Share on other sites

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.