简体   繁体   中英

PHP multidimesion array into html table

I have an array of data as below

   $array = array(
       'xcol'=>array('no','head','head1','head2'=>array(
          'o1','o2'
       ),'head3'), // => convert into th
       'ycol'=>array(                                      // => convert into td
          '1'=>array(
              'name1'=>array('data1',array('a','1'),'data3')
          ),
          '2'=>array(
              'name2'=>array('data1',array('b','2'),'data3')
          ),
          '3'=>array(
              'name3'=>array('data1',array('c','3'),'data3')
          ),
          '4'=>array(
              'name4'=>array('data1',array('d','4'),'data3')
          ),
          '5'=>array(
              'name5'=>array('data1',array('e','5'),'data3')
          )
       )
    );

And I want to turn it into html table as below.

**no** | **head**  | head1 |  head2  | head3
       |           |       | o1 | o2 |
--------------------------------------------
**1**  | **name1** | data1 | a  | 1  | data3
**2**  | **name2** | data1 | b  | 2  | data3
**3**  | **name3** | data1 | c  | 3  | data3
**4**  | **name4** | data1 | d  | 4  | data3
**5**  | **name5** | data1 | e  | 5  | data3

if anyone can help me to solve it. Thanks

This is what I can come up with so far. You can simplify it further by combining the functions and also use a recursive function to iterate through all the array elements. Hope this helps.

// Separate the xcol & ycol 
$array_x = $array['xcol'];
$array_y = $array['ycol'];

create_table(array_x($array_x),array_y($array_y));

function array_x($input = array()){
    $str = '<tr>';
    if (is_array($input)){
        $str2 .= "<tr>";
        foreach ($input as $key=>$value){
            if (is_array($value)){
                $str .= "<th colspan='2'>".$key."</th>";
                foreach ($value as $k=>$v){
                    $str2 .= "<th>".$v."</th>"; 
                }
            }else{
                $str .= "<th>".$value."</th>";
                $str2 .= "<th>&nbsp;</th>";
            }
        }
    }
    $str2 .= '</tr>'."\n";
    $str .= '</tr>'."\n".$str2;
    return $str;
}

function array_y($input = array()){
    $str = "";
    if (is_array($input)){
        foreach ($input as $key=>$value){
            if (is_array($value)){
                $str .= "<tr><td>".$key."</td>";
                foreach ($value as $k=>$v){
                    if (is_array($v)){
                        $str .= "<td>".$k."</td>";
                        foreach ($v as $k1=>$v1){
                            if (is_array($v1)){
                                foreach ($v1 as $k2=>$v2){
                                    $str .= "<td>".$v2."</td>"; 
                                }
                            }else{
                                $str .= "<td>".$v1."</td>"; 
                            }
                        }
                    }else{
                        $str .= "<td>".$v."</td>"; 
                    }
                }
            }else{
                $str .= "<td>".$value."</td>";
            }
        }
    }
    $str .= '</tr>'."\n";
    return $str;
}

function create_table($str_x,$str_y){
    $str = '<table border="1">'."\n";
    $str .=  $str_x.$str_y;
    $str .= '</table>'."\n";
    echo $str;
}

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