dqhr76378 2017-12-22 07:04
浏览 41

将关联数组打印到表

I have an associative array like this, i want to generate a table with these data, like breakfast, snacks, lunch, supper, dinner in row the following is the code I've tried, but I am stuck where to break the table row because when the array contains more than one item.i wants to Print associative array to table

*--------------------------------------------------*
| **Breakfast   Snacks  Lunch   Supper   Dinner**  |
| test          test            test     testfrom
|testfrom
*--------------------------------------------------*

array output is as follow

    Array
(
    [meal_plan_id] => 17
    [calorie_limit] => 1
    [total_calorie] => 0
    [date] => 2017-12-29
    [meal_plan] => Array
        (
            [0] => Array
                (
                    [meal_type] => bf
                    [label] => Breakfast
                    [calorie_limit] => 30
                    [total_calorie] => 0
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 107
                                    [label] => test
                                    [quantity] => 10
                                    [unit] => g
                                    [status] => bf
                                )

                            [1] => Array
                                (
                                    [id] => 109
                                    [label] => testfrom
                                    [quantity] => 12
                                    [unit] => g
                                )

                        )

                )

            [1] => Array
                (
                    [meal_type] => sn
                    [label] => Snacks
                    [calorie_limit] => 10
                    [total_calorie] => 0
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 108
                                    [label] => test
                                    [quantity] => 121
                                    [unit] => g
                                )

                        )

                )

            [2] => Array
                (
                    [meal_type] => lu
                    [label] => Lunch
                    [calorie_limit] => 20
                    [total_calorie] => 0
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [status] => su
                                )

                        )

                )

            [3] => Array
                (
                    [meal_type] => su
                    [label] => Supper
                    [calorie_limit] => 30
                    [total_calorie] => 0
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [status] => sn
                                )

                            [1] => Array
                                (
                                    [id] => 116
                                    [label] => test
                                    [quantity] => 200
                                    [unit] => oz
                                )

                        )

                )

            [4] => Array
                (
                    [meal_type] => dn
                    [label] => Dinner
                    [calorie_limit] => 20
                    [total_calorie] => 0
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 113
                                    [label] => test500
                                    [quantity] => 20
                                    [unit] => oz
                                    [status] => dn
                                )

                        )

                )

        )

)

below is the code I've tried

// $daily_meals = show_daily_meals() // contains the array 
    if(!empty($daily_meals['meal_plan'])){

         echo '<tr>';
        foreach($daily_meals['meal_plan'] as $meal_plan){      

             foreach ($meal_plan['data'] as $data){

                if(!empty($data['id']))
                    echo '<td class="'.$meal_type.'" data-meals-id="'.$data['id'].'"><span class="left">'.$data['label'].'</span> <span class="right">'.$data['quantity'].' <a class="delete_row"><i class="fa fa-trash" aria-hidden="true"></i></a></span><div class="row_loader"></div></td>';

               else echo '<td></td>'; 
            }

            $i++;
            if($i%5 == 0) echo '</tr>';
        }  

    }
  • 写回答

2条回答 默认 最新

  • douao1854 2017-12-22 07:25
    关注

    Try storing all your column names in an array and create a table with it:

    foreach($array as $row) {
        $column[$i]['label'] = $row['label'];
        $column[$i]['data'] = $this->getdata($row['data']); 
        $i++;
    }
    
    function getdata($array) {
        $data = '';
    
        foreach($array as $row) {
            $data. = $row['label'].',';  
        }
    
        return $data;
    }
    

    Create a table with the column names and try inserting data using a loop with respect to insert into

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100