简体   繁体   中英

PHP Multidimensional associative array to HTML table

I have a multidimensional array in PHP that I would like to put in to a table in HTML. This is the array I have:

[2014-04-04] => Array
    (
        [0] => Array
            (
                [name] => Bob
                [uid] => 4tVydHSaLFwm9QbGfYtzwoyV5bfvEGn8
                [result] => 10.4
                [type] => a_breakfast
            )

        [1] => Array
            (
                [name] => Bob
                [uid] => 4tVydHSaLFwm9QbGfYtzwoyV5bfvEGn8
                [result] => 3.9
                [type] => b_lunch
            )

    )

[2014-04-05] => Array
    (
        [0] => Array
            (
                [name] => Bob
                [uid] => 4tVydHSaLFwm9QbGfYtzwoyV5bfvEGn8
                [result] => 3.5
                [type] => b_breakfast
            )

    )

How can I get it so that the table has the following:

|   DATE     |    BEFORE BREAKFAST  | AFTER BREAKFAST | BEFORE LUNCH | AFTER .... 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 2014-04-04 |                      |      10.4       |     3.9      |
| 2014-04-05 |       3.5            |                 |              |

I have tried multiple ways but I can not get it to work!

Use this Code hope this will help you . if not then you can get at least a basic idea from it .

<?php

  $values = array(
   'Index' => array(
        'Date' => '10-10-13',
       'Name' => 'Azi Baloch',
       'Number' => '12345'
    ),
   'Index2' => array(
        'Date' => '11-10-13',
        'Name' => 'Asad Butt',
        'Number' => '123321'
    )
    );

 $array = array_values($values);


  // Genrate HTML
  echo '<table border=1 width=800>';
  echo '<tr><td>Category</td><td>Name</td><td>Number</td></tr>';
  for($i = 0; $i< count($array); $i++) {
    echo '<tr>';
    echo '<td>'.$array[$i]['Date'].'</td>';
    for($j = 1; $j<count($array[$i]); $j++) {
        $n = array_values($array[$i]);

        echo '<td>'. $n[$j] .'</td>';
    }
    echo '</tr>';
  }
  echo '</table>';

 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM