1

I have a multidimensional array with x number of arrays inside of it. Each internal array has 30 values. What I need to do is print each array's values vertically in columns as table rows then have a new column created for each array.

my array looks like this

Array ( 
[0] => Array ( [0] => 0.00 [1] => 0.00 [2] => 0.00 etc...) 

[1] => Array ( [0] => 0.00 [1] => 0.00 [2] => 0.00 etc... ) 

[2] => Array ( [0] => 0.00 [1] => 0.00 [2] => etc... ) 

[3] => Array ( [0] => 0.00 [1] => 0.00 [2] => 0.00 etc...) )

I need the table to look something like this

  Column 1 "array 0"   | Column 2 "array 1"    | Column 3 "array 2"  

array [0]([1]=> value  | array [1]([1]=> value | array [2]([1]=> value  
array [0]([2]=> value  | array [1]([2]=> value | array [2]([3]=> value   
array [0]([3]=> value  | array [1]([3]=> value | array [2]([3]=> value   
etc....

I have tried different foreach loops but can't get the result I'm looking for.

Here is the code that I am currently using, but fails.

foreach($pension as $key){
        echo "<tr>";
        foreach($key as $value){
            echo "<td>$value</td>";

        }
    echo "</tr>";
    }

Printing the data horizontally would be easier, but I know there must be a solution. Any help would be great!

Andrew Brown
  • 125
  • 14

5 Answers5

2

You'd be better off restructuring your array before trying to output it. Something like this should work:

$output = array();
foreach ($pension as $a) {
    foreach ($a as $key => $value) {
        $output[$key][] = $value; 
    }
}

Then output it:

foreach ($output as $o) {
    echo '<tr><td>' . implode('</td><td>', $o) . '</td></tr>';
}

Here's an example

billyonecan
  • 20,090
  • 8
  • 42
  • 64
  • Yeah that worked man. Thank you! It prints just how I want it. Now the next challenge is to have that print side by side in the same table with other arrays that generate the same way. Gah! Maybe include all the other arrays of values into one massive multidimensional array then output like you mentioned. Ill fool around with it. – Andrew Brown Jul 24 '15 at 21:57
  • 1
    Assumes arrays are same length, easy enough to address though. Also be good to plug [`array_column`](http://php.net/manual/en/function.array-column.php) here. – ficuscr Jul 24 '15 at 21:58
0

Sorta ugly but this works. Nesting tables. live here.

<?php
$foo = [1 => ['1-1', '1-2', '1-3'],
        2 => ['2-1', '2-2', '2-3', '2-4', '2-5'],
        3 => ['3-1', '3-2', '3-3', '3-4'],
        4 => ['4-1', '4-2',]
       ];
?>

<table>
    <tr style="vertical-align:top;">
        <?php foreach ($foo as $col): ?>
        <td>
            <table>
                <?php foreach ($col as $cell): ?>
                <tr>
                    <td>
                        <?= $cell ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </table>
        </td>
        <?php endforeach; ?>
    </tr>
</table>
ficuscr
  • 6,975
  • 2
  • 32
  • 52
0

I haven't tested this, but I think this should work. This assumes that there will always be 30 values in each array.

for ($i = 0 ; $i < 30 ; $i++) {
    echo '<tr>';

    foreach ($pension as $array) {
        echo "<td>{$array[$i]}</td>";
    }

    echo '</tr>';
}
0

This works too. Using array_columns, you can pull out the values from each array and stick it in a new array then use a for loop to walk through them all and print them out in columns.

$zeroColumn = array_column($array, 0);
$oneColumn = array_column($array, 1);
$twoColumn = array_column($array, 2);

$count = count($zeroColumn);

for ($i=0; $i<$count; $i++) {
    echo $zeroColumn[$i] . " | " . $oneColumn[$i] . " | " . $twoColumn[$i] . "</br>";
}
Shavez00
  • 11
  • 3
-1

You can transpose the array (so columns become rows and vice versa), and then printing the data should be trivial. Unfortunately PHP does not ship with a transpose function, so you need to write your own (or use this one).

Anonymous
  • 11,740
  • 3
  • 40
  • 50