如何在第一行中显示第一个数组,然后在第二行中显示第二个数组,然后在第三行中显示第三个数组

时间:2019-12-28 11:22:47

标签: php

如何在第一行中显示第一个数组,然后在第二行中显示第二个数组,然后在第三行中显示第三个数组

    Array
    (
    [S] => Array
        (
            [0] => Array
                (
                    [id] => 61
                    [area_code] => SS-5
                )

        )
    [A] => Array
        (
            [0] => Array
                (
                    [id] => 24
                    [area_code] => AHUP-9
                )
        )

    )

enter image description here

1 个答案:

答案 0 :(得分:0)

结合使用简单的foreach循环:

foreach($data as $upkey=>$set){      
    foreach($set as $ind=>$value){  

        echo $value['area_code'];  

        if (isset($set[$ind+1])) echo '-----';
    }   
    echo PHP_EOL;  
}

Demo

如果您需要一个包含列和行的表,则可以使用简单的示例,例如:

// open table(thead included) and tbody tags
echo '<table class="table"><thead></thead><tbody>';       

foreach($data as $set){    
    // open row
    echo '<tr>';                            
    foreach($set as $value){   

        // print column with value
        echo '<td>'.$value['area_code'].'</td>'; 
    }   

    // close row
    echo '</tr>';                           
}

// close tbody and table tags
echo '</tbody></table>';  

Demo