doukenqiong0588 2015-08-09 08:49
浏览 38
已采纳

Foreach多维数组

I have the following output created from a PHP script I have created:

Array
(
[cage] =>  OK
[logicaldrive] => Array
    (
        [logicaldrive 1] => Array
            (
                [size] => 2.7 TB
                [raid] => RAID 1
                [status] => OK
            )
    )

[physicaldrive] => Array
    (
        [logicaldrive 1] => Array
            (
                [physicaldrive 2I:1:1] => Array
                    (
                        [bay] => port 2I:box 1:bay 1
                        [type] => SATA
                        [size] => 3 TB
                        [status] => OK
                    )

                [physicaldrive 2I:1:2] => Array
                    (
                        [bay] => port 2I:box 1:bay 2
                        [type] => SATA
                        [size] => 3 TB
                        [status] => OK
                    )
            )

    )

I would like to be able to format this data into a table, I have tried but unable to run a foreach loop on sections of the array, for example if I want a table to have the information from:

[logicaldrive] => Array
    (
        [logicaldrive 1] => Array
            (
                [size] => 2.7 TB
                [raid] => RAID 1
                [status] => OK
            )

    )

I can't work out how to get the values of size, raid, status without knowing the key (logicaldrive 1) which depending on the system that the code has run on will be different each time.

  • 写回答

1条回答 默认 最新

  • dongxue163536 2015-08-09 08:59
    关注

    Try this...

    $all_data; //your complete array
    
    foreach($all_data as $key=>$sub_array){
       if($key === "logicaldrive"){
           foreach($sub_array as $key=>$sub_sub_array){
               echo $key;//logicaldrive 1
               foreach($sub_sub_array as $key=>$fact){
                   echo '<td>'.$key.'='.$fact.'<td>';//size=2.7 TB,raid=RAID 1, status=OK
               }
           }
       }
    }
    

    Obviously you will need to wrap the td elements appropriately this is just an example to get you going with the logic

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?