0

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

3
  • 2
    What have you tried so far? What's the problem? You have a well structured array which describe your table nicely. You'll need to iterate through as described? Commented Mar 24, 2011 at 5:47
  • php.net/manual/en/control-structures.foreach.php... Commented Mar 24, 2011 at 5:52
  • sorry guys i am newbie here, anyway thanks for your instruction. Commented Mar 24, 2011 at 12:45

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.