如何在PHP中打印以下多维数组?

时间:2017-03-21 10:14:09

标签: php html codeigniter-2

我想以表格格式打印这些数据,每列包含1个日期,如1,2,3,...............,31

[{"empId":"47","name":"Kalyani","dateStatus":[{"presentStatus":0,"attDate":"2017-03-01"},{"presentStatus":0,"attDate":"2017-03-02"},{"presentStatus":0,"attDate":"2017-03-03"},{"presentStatus":0,"attDate":"2017-03-04"},{"presentStatus":0,"attDate":"2017-03-05"},{"presentStatus":0,"attDate":"2017-03-06"},{"presentStatus":0,"attDate":"2017-03-07"},{"presentStatus":0,"attDate":"2017-03-08"},{"presentStatus":0,"attDate":"2017-03-09"},{"presentStatus":0,"attDate":"2017-03-10"},{"presentStatus":0,"attDate":"2017-03-11"},{"presentStatus":0,"attDate":"2017-03-12"},{"presentStatus":0,"attDate":"2017-03-13"},{"presentStatus":0,"attDate":"2017-03-14"},{"presentStatus":0,"attDate":"2017-03-15"},{"presentStatus":0,"attDate":"2017-03-16"},{"presentStatus":1,"attDate":"2017-03-17"},{"presentStatus":1,"attDate":"2017-03-18"},{"presentStatus":0,"attDate":"2017-03-19"},{"presentStatus":0,"attDate":"2017-03-20"},{"presentStatus":0,"attDate":"2017-03-21"},{"presentStatus":0,"attDate":"2017-03-22"},{"presentStatus":0,"attDate":"2017-03-23"},{"presentStatus":0,"attDate":"2017-03-24"},{"presentStatus":0,"attDate":"2017-03-25"},{"presentStatus":0,"attDate":"2017-03-26"},{"presentStatus":0,"attDate":"2017-03-27"},{"presentStatus":0,"attDate":"2017-03-28"},{"presentStatus":0,"attDate":"2017-03-29"},{"presentStatus":0,"attDate":"2017-03-30"},{"presentStatus":0,"attDate":"2017-03-31"}]}]

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

你必须将json编码的字符串解码为数组,然后才能打印

 $json = '[{"empId":"47","name":"Kalyani","dateStatus":[{"presentStatus":0,"attDate":"2017-03-01"},{"presentStatus":0,"attDate":"2017-03-02"},{"presentStatus":0,"attDate":"2017-03-03"}]}]';   

 $json_array = json_decode($json,true);
 print_r('<pre>');
 print_r(json_decode($json,true));
 print_r('<pre>');

 output will 

 Array
  (
   [0] => Array
      (
        [empId] => 47
        [name] => Kalyani
        [dateStatus] => Array
            (
                [0] => Array
                    (
                        [presentStatus] => 0
                        [attDate] => 2017-03-01
                    )

                [1] => Array
                    (
                        [presentStatus] => 0
                        [attDate] => 2017-03-02
                    )

                [2] => Array
                    (
                        [presentStatus] => 0
                        [attDate] => 2017-03-03
                    )
               )
       )
 )

答案 2 :(得分:0)

简单的HTML表格,假设您的字符串的第一个节点包含1,2 ..... 31个日期。

$a = json_decode('[{"empId":"47","name":"Kalyani","dateStatus":[{"presentStatus":0,"attDate":"2017-03-01"},{"presentStatus":0,"attDate":"2017-03-02"},{"presentStatus":0,"attDate":"2017-03-03"},{"presentStatus":0,"attDate":"2017-03-04"},{"presentStatus":0,"attDate":"2017-03-05"},{"presentStatus":0,"attDate":"2017-03-06"},{"presentStatus":0,"attDate":"2017-03-07"},{"presentStatus":0,"attDate":"2017-03-08"},{"presentStatus":0,"attDate":"2017-03-09"},{"presentStatus":0,"attDate":"2017-03-10"},{"presentStatus":0,"attDate":"2017-03-11"},{"presentStatus":0,"attDate":"2017-03-12"},{"presentStatus":0,"attDate":"2017-03-13"},{"presentStatus":0,"attDate":"2017-03-14"},{"presentStatus":0,"attDate":"2017-03-15"},{"presentStatus":0,"attDate":"2017-03-16"},{"presentStatus":1,"attDate":"2017-03-17"},{"presentStatus":1,"attDate":"2017-03-18"},{"presentStatus":0,"attDate":"2017-03-19"},{"presentStatus":0,"attDate":"2017-03-20"},{"presentStatus":0,"attDate":"2017-03-21"},{"presentStatus":0,"attDate":"2017-03-22"},{"presentStatus":0,"attDate":"2017-03-23"},{"presentStatus":0,"attDate":"2017-03-24"},{"presentStatus":0,"attDate":"2017-03-25"},{"presentStatus":0,"attDate":"2017-03-26"},{"presentStatus":0,"attDate":"2017-03-27"},{"presentStatus":0,"attDate":"2017-03-28"},{"presentStatus":0,"attDate":"2017-03-29"},{"presentStatus":0,"attDate":"2017-03-30"},{"presentStatus":0,"attDate":"2017-03-31"}]},{"empId":"48","name":"XYZ","dateStatus":[{"presentStatus":1,"attDate":"2017-03-01"},{"presentStatus":0,"attDate":"2017-03-02"},{"presentStatus":0,"attDate":"2017-03-03"},{"presentStatus":0,"attDate":"2017-03-04"},{"presentStatus":0,"attDate":"2017-03-05"},{"presentStatus":0,"attDate":"2017-03-06"},{"presentStatus":0,"attDate":"2017-03-07"},{"presentStatus":0,"attDate":"2017-03-08"},{"presentStatus":0,"attDate":"2017-03-09"},{"presentStatus":0,"attDate":"2017-03-10"},{"presentStatus":0,"attDate":"2017-03-11"},{"presentStatus":0,"attDate":"2017-03-12"},{"presentStatus":0,"attDate":"2017-03-13"},{"presentStatus":0,"attDate":"2017-03-14"},{"presentStatus":0,"attDate":"2017-03-15"},{"presentStatus":0,"attDate":"2017-03-16"},{"presentStatus":1,"attDate":"2017-03-17"},{"presentStatus":1,"attDate":"2017-03-18"},{"presentStatus":0,"attDate":"2017-03-19"},{"presentStatus":0,"attDate":"2017-03-20"},{"presentStatus":0,"attDate":"2017-03-21"},{"presentStatus":0,"attDate":"2017-03-22"},{"presentStatus":0,"attDate":"2017-03-23"},{"presentStatus":0,"attDate":"2017-03-24"},{"presentStatus":0,"attDate":"2017-03-25"},{"presentStatus":0,"attDate":"2017-03-26"},{"presentStatus":0,"attDate":"2017-03-27"},{"presentStatus":0,"attDate":"2017-03-28"},{"presentStatus":0,"attDate":"2017-03-29"},{"presentStatus":0,"attDate":"2017-03-30"},{"presentStatus":0,"attDate":"2017-03-31"}]}]',true);
echo '<table border=1>';
foreach($a as $k => $v)
{
        echo '<tr>';
        echo '<td>Name/Date</td>';
        echo  '';
        foreach($v['dateStatus'] as $k1 => $v1)
        {   
            echo '<td>';
                echo date('d-M ', strtotime($v1['attDate']));
            echo '</td>';
        }
        echo '</tr>';
        break;
}
foreach($a as $b => $c)
{
    echo '<tr>';
        echo '<td>'.$c['name'].'('.$c['empId'].')'.'</td>';
        foreach($c['dateStatus'] as $k1 => $v1)
        {   
            echo '<td>';
                echo $v1['presentStatus'];
            echo '</td>';
        }
    echo '</tr>';
}
echo  '</table>';