foreach和嵌套数组的问题

时间:2015-04-06 01:45:38

标签: php arrays for-loop foreach

我在PHP中遇到foreach和嵌套arrays的问题。我得到了整个foreach ($var as $newvar)等等,但是我遇到了一个新的要求,我似乎无法弄清楚/ vizualize。这是一个简单的查询:

select systems.hostname, capacity_disk.server_id, capacity_disk.disk_mountpoint, capacity_disk.disk_datetime, capacity_disk.disk_device, capacity_disk.disk_capacity, capacity_disk.disk_used, capacity_disk.disk_used_percent from systems left join capacity_disk on systems.id=capacity_disk.server_id where capacity_disk.disk_datetime = UNIX_TIMESTAMP(subdate(current_date,3)) and systems.id=44;

我得到以下结果:

+--------------------------------+-----------+-----------------+---------------+----------------------------+---------------+------------+-------------------+
| hostname                       | server_id | disk_mountpoint | disk_datetime | disk_device                | disk_capacity | disk_used  | disk_used_percent |
+--------------------------------+-----------+-----------------+---------------+----------------------------+---------------+------------+-------------------+
| server01.example.org           |        44 | /backup         |    1427950800 | /dev/mapper/storage-backup | 1870561476    | 1437491340 | 81%               | 
| server01.example.org           |        44 | /               |    1427950800 | /dev/mapper/storage-root   | 15126920      | 4286048    | 30%               | 
+--------------------------------+-----------+-----------------+---------------+----------------------------+---------------+------------+-------------------+
2 rows in set (0.01 sec)

所有这些数据都是预期的并且正在发挥作用。

问题是我无法找出为每个主机名运行foreach的PHP逻辑,然后遍历每个disk_mountpoint。显然,第一部分很简单:

$query = $db->Execute("select systems.hostname, capacity_disk.server_id, capacity_disk.disk_mountpoint, capacity_disk.disk_datetime, capacity_disk.disk_device, capacity_disk.disk_capacity, capacity_disk.disk_used, capacity_disk.disk_used_percent from systems left join capacity_disk on systems.id=capacity_disk.server_id where capacity_disk.disk_datetime = UNIX_TIMESTAMP(subdate(current_date,3)) and systems.id=44;");

foreach ($query as $server) {

}

此时我可以通过$server['hostname']引用主机名 - 这也很好。问题是 - 对于每个$server['hostname']个项目(此结果集中的1个服务器,但是我还希望迭代2个项目),我想迭代这两个{{1}并且能够操纵数据库中表行中的数据。另一个问题是,我需要能够报告我的所有项目并返回以下格式的内容:

disk_mountpoint

因此,这两个项目的样本输出将是:

'`disk_mountpoint`','`disk_capacity`','`disk_used_percent`'

我从来没有在PHP中做到这一点,并且很难弄清楚如何将逻辑放到代码中。

1 个答案:

答案 0 :(得分:0)

根据您的描述和示例输出,您不必完全清楚为什么需要将其视为嵌套数组,但由于数据库查询始终返回单个列表,因此您首先必须遍历结果以将其转换为一个嵌套的数组,如下所示,然后你可以遍历它来做你需要的。

$list_by_server = array(); // initialize an array to organize results by host name
foreach ($query as $row) {
    $list_by_server[$row['hostname']] = $row;
}

foreach ($list_by_server as $server) {
    // any per server setup and actions go here
    foreach ($server as $disk) {
        // process disk details and update database
        // output disk details
    }
}

您还可以设置一个变量来跟踪当前服务器,如下所示,并避免两个foreach循环。

$current = ''; // initialize tracking car
foreach ($query as $row) {
    if ($current == $row['hostname']) {
        // do something with another row from the current host
    } else  {
        // it's a new host 
        // do something with the row
        $current == $row['hostname']; // updates tracking var 
    }
}