具有多个键的多维数组输出到foreach循环

时间:2013-03-06 22:24:10

标签: php arrays multidimensional-array foreach

我有一个MD数组,我需要运行一个函数来返回结果。 将其置于上下文中,其5台服务器具有1-3个硬盘驱动器,可以获得剩余空间。

$array = array(
"Server 1" => array("C" => "85791338496", "D" => "322119397376"),
"Server 2" => array("C" => "268327448576", "E" => "536733544448", "H" => "274874757120"),
"Server 3" => array("C" => "42947571712", "E" => "214744166400"),
"Server 4" => array("C" => "64317550592", "D" => "150320705536"),
"Server 5" => array("C" => "64317550592")
);

它需要遍历每个服务器,然后每个硬盘驱动器返回可用空间。 执行此操作的功能已受到控制,但基于上述数组的循环是我被卡住的地方。

1 个答案:

答案 0 :(得分:4)

foreach ($array as $server => $disks) {
    print $server .' has the following disks:';
    foreach ($disks as $disk => $avalable_space) {
        print $disk .' has '. $available_space . 'available space';
    }
}