Print multidimensional array to table

你。 提交于 2019-12-24 22:42:59

问题


I have a array like this,

Array
(
    [0] => Array
        (
            [id] => 109
            [label] => c
            [quantity] => 12
            [unit] => g
        )

    [1] => Array
        (
            [id] => 120
            [label] => c
            [quantity] => 12
            [unit] => g
        )

    [2] => Array
        (
        )

    [3] => Array
        (
        )

    [4] => Array
        (
            [id] => 119
            [label] => b
            [quantity] => 200
            [unit] => oz
        )

)

Array
(
    [0] => Array
        (
            [id] => 118
            [label] => b
            [quantity] => 200
            [unit] => oz
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

    [3] => Array
        (
        )

    [4] => Array
        (
        )

)

Array
(
    [0] => Array
        (
            [id] => 121
            [label] => b
            [quantity] => 10
            [unit] => g
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

    [3] => Array
        (
        )

    [4] => Array
        (
        )

)

I have to print like

<table>
<tr><td>c</td><td>c</td><td></td><td></td><td>b</td></tr>
<tr><td>b</td><td></td><td></td><td></td><td></td></tr>
<tr><td>b</td><td></td><td></td><td></td><td></td></tr>
</table>

what I tried is,

foreach($final_array as $food_array){
echo '<tr>';
echo '<td>'.$food_array[0]['label'].'</td>';
echo '<tr>';
}

but it seems not working, can anyone help in this?


回答1:


Here is the code you should try,

foreach ($final_array as $food_array) {
    echo '<tr>';
    foreach ($food_array as $key1 => $value1) {
        echo '<td>' . $value1['label'] . '</td>';    
    }
    echo '<tr>';
}

You need to loop it twice to get your output.



来源:https://stackoverflow.com/questions/47978473/print-multidimensional-array-to-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!