将关联数组打印到表

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>';
        }  

    }

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

Here is your solution

Input

<?php 
    $array = array(
        array(
            'meal_type' => 'bf',
            'label' => 'Breakfast',
            'calorie_limit' => 30,
            'total_calorie' => 0,
            'data' => array(
                array(
                    'id' => 107,
                    'label' => 'test',
                    'quantity' => 10,
                    'unit' => 'g',
                    'status' => 'bf'
                ),
                array(
                    'id' => 109,
                    'label' => 'testfrom',
                    'quantity' => 12,
                    'unit' => 'g'
                )
            )
        ),
        array(
            'meal_type' => 'sn',
            'label' => 'Snacks',
            'calorie_limit' => 10,
            'total_calorie' => 0,
            'data' => array(
                array(
                    'id' => 108,
                    'label' => 'test',
                    'quantity' => 121,
                    'unit' => 'g'
                )
            )
        ),
        array(
            'meal_type' => 'lu',
            'label' => 'Lunch',
            'calorie_limit' => 20,
            'total_calorie' => 0,
            'data' => array(array('status' => 'su'))
        ),
        array(
            'meal_type' => 'su',
            'label' => 'Supper',
            'calorie_limit' => 30,
            'total_calorie' => 0,
            'data' => array(
                array('status' => 'sn'),
                array(
                    'id' => 116,
                    'label' => 'test',
                    'quantity' => 200,
                    'unit' => 'oz'
                ),
            )
        ),
        array(
            'meal_type' => 'dn',
            'label' => 'Dinner',
            'calorie_limit' => 20,
            'total_calorie' => 0,
            'data' => array(
                array(
                    'id' => 113,
                    'label' => 'test500',
                    'quantity' => 20,
                    'unit' => 'oz',
                    'status' => 'dn'
                )
            )
        )
    );

Solution

1st foreach for parent array. Then second for its child..

    foreach($array as $r){
        $$r['label'] = '<table width="100%">';
        foreach($r['data'] as $s){
            if(isset($s['label']))$$r['label'] .= '<tr><td align="center">'.$s['label'].'</td></tr>'; // The if condition check weather the label is exist or not if yes then add that in particular label's table
        }
        $$r['label'] .= '</table>';
    }
    echo '
    <table width="100%" border="1">
        <thead>
            <tr>
                <th>Breakfast</th>
                <th>Snacks</th>
                <th>Lunch</th>
                <th>Supper</th>
                <th>Dinner</th>
            </tr>
        </thead>

Here all labels canverted into variable by $$r['label']. And all have there own table Now we add all these tables into the master table to get the output.

        <tbody>
            <tr>
                <td>'.$Breakfast.'</td>
                <td>'.$Snacks.'</td>
                <td>'.$Lunch.'</td>
                <td>'.$Supper.'</td>
                <td>'.$Dinner.'</td>
            </tr>
        </tbody>

    </table>';
    //?//    echo "<pre>";print_r($array);

    ?>

Output

Output