0

I need some help, I am trying to create a dynamic HTML table that will display some results from a nested array.

My array will always be setup like this:

Array
(
    [column_label] => Size No.
    [column_data] => Array
        (
            [0] => Array
                (
                    [column_cell] => #3
                )

            [1] => Array
                (
                    [column_cell] => #3.5
                )

            [2] => Array
                (
                    [column_cell] => #4
                )

            [3] => Array
                (
                    [column_cell] => #4.5
                )

        )

)

Array
(
    [column_label] => Approx. Diameter Inches
    [column_data] => Array
        (
            [0] => Array
                (
                    [column_cell] => 3/32"
                )

            [1] => Array
                (
                    [column_cell] => 7/64"
                )

            [2] => Array
                (
                    [column_cell] => 1/8"
                )

            [3] => Array
                (
                    [column_cell] => 9/64"
                )

        )

)

Array
(
    [column_label] => Approx. Diameter mm
    [column_data] => Array
        (
            [0] => Array
                (
                    [column_cell] => 2.38
                )

            [1] => Array
                (
                    [column_cell] => 2.78
                )

            [2] => Array
                (
                    [column_cell] => 3.18
                )

            [3] => Array
                (
                    [column_cell] => 3.57
                )

        )

)

Array
(
    [column_label] => Catalog No.
    [column_data] => Array
        (
            [0] => Array
                (
                    [column_cell] => 32030
                )

            [1] => Array
                (
                    [column_cell] => 32035
                )

            [2] => Array
                (
                    [column_cell] => 32040
                )

            [3] => Array
                (
                    [column_cell] => 32045
                )

        )

)

I need to go through each [column_data] array and prepare them to display as table cells in my table.

For example a row in my table should include table cells from the column_data array with a key of 0. The next row in my table will include table cells from the column_data array with a key of 1 and so on.

Hope this makes sense, this way Im sure the cells will always correspond to Column_label table heading and be displayed vertically in cells.

Here is my failed attempt (unfinished):

<?php if ($product_table) { 
    //Setup empty arrays
    $products = array();
    $labels = array();
    $data = array();
    $column_cells = array();

    foreach($product_table as $product) { 
        //echo '<pre>';
        //print_r($product);
        //echo '</pre>';
        //echo '<th>' . $product['column_label']  . '</th>';
        $products[] = $product;
        $labels[] = $product['column_label'];
        $data[] = $product['column_data'];

            foreach ($product['column_data'] as $column_data) {
                $column_cells[] = $column_data;
            }
        } 

?>  

<table class="table">
    <tr>
        <?php foreach($labels as $label) {
            echo '<th>' . $label . '</th>';
        } ?>
    </tr>

    <?php foreach($products as $product) { ?>
    <tr>
        <?php 
            echo '<pre>';
            print_r($product);
            echo '</pre>';
        ?>
    </tr>
    <?php } ?>
</table>

<?php } ?>
2
  • Do you have any control on how the initial array is setup? Just curious because having it organized by data columns rather than rows in an associated array makes it a little more complicated than it needs to be, and much more difficult to read. Commented Mar 10, 2015 at 18:09
  • Not really, if I print the products variable instead of product then each product is numbered, not sure if that helps. In a nutshell I just the column_label to be the table heading and the info in column_data to be displayed vertically under the corresponding table heading. I thought of using array_merge or using a for loop and counting. Still not sure and have put way too much time into trying to figure it out. Commented Mar 10, 2015 at 18:14

1 Answer 1

1

You need to loop over your column_data, and using the array key, add the column_cell to the row based off it's key.

$labels = array();
$rows = array();

foreach($product_table as $product) { 

    $labels[] = $product['column_label'];

    foreach ($product['column_data'] as $key => $column_data) {
        $rows[$key][] = $column_data['column_cell'];
    }

} ?>  

Then in you table, you need to loop over each row array, and print the cell data

<table class="table">
    <tr>
        <?php foreach($labels as $label) {
            echo '<th>' . $label . '</th>';
        } ?>
    </tr>
    <?php foreach($rows as $row) { ?>
    <tr>
        <?php foreach($row as $cell) {
            echo '<td>' . $cell . '</td>';
        } ?>
    </tr>
    <?php } ?>
</table>

Using your sample data -

$product_table = array(
    array('column_label' => 'Size No.',
          'column_data' => array(
                0 => array('column_cell' => '#3'),
                1 => array('column_cell' => '#3.5'),
                2 => array('column_cell' => '#4'),
                3 => array('column_cell' => '#4.5')  )      
        ),
    array('column_label' => 'Approx. Diameter Inches',
          'column_data' => array(
                0 => array('column_cell' => '3/32"'),
                1 => array('column_cell' => '7/64"'),
                2 => array('column_cell' => '1/8"'),
                3 => array('column_cell' => '9/64"'))
        ),
    array('column_label' => 'Approx. Diameter mm',
          'column_data' => array(
                0 => array('column_cell' => '2.38'),
                1 => array('column_cell' => '2.78'),
                2 => array('column_cell' => '3.18'),
                3 => array('column_cell' => '3.57') )

        ),
    array('column_label' => 'Catalog No.',
          'column_data' => array(
                0 => array('column_cell' => '32030'),
                1 => array('column_cell' => '32035'),
                2 => array('column_cell' => '32040'),
                3 => array('column_cell' => '32045')  )

        )

);

You end up with this result enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

You sir, are a saint.
always glad to help, happy coding to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.