麻烦的foreach和嵌套数组 [英] Trouble with foreach and nested arrays

查看:226
本文介绍了麻烦的foreach和嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与的foreach 嵌套的阵列在PHP是一个问题,。我得到整个的foreach($ var当成$ newvar)等,但我遇到了我似乎无法找出/ vizualize了新的要求。这里有一个简单的查询:

 选择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留在加盟capacity_disk systems.id = capacity_disk.server_id哪里capacity_disk.disk_datetime = UNIX_TIMESTAMP(SUBDATE(CURRENT_DATE,3))和systems.id = 44;

我得到以下结果:

<$p$p><$c$c>+--------------------------------+-----------+-----------------+---------------+----------------------------+---------------+------------+-------------------+
|主机名| SERVER_ID | disk_mountpoint | disk_datetime | disk_device | disk_capacity | disk_used | disk_used_percent |
+--------------------------------+-----------+-----------------+---------------+----------------------------+---------------+------------+-------------------+
| server01.example.org | 44 | /备份| 1427950800 |为/ dev /映射器/存储备份| 1870561476 | 1437491340 | 81%|
| server01.example.org | 44 | / | 1427950800 |为/ dev /映射器/存储根| 15126920 | 4286048 | 30%|
+--------------------------------+-----------+-----------------+---------------+----------------------------+---------------+------------+-------------------+
2排在套(0.01秒)

所有这些数据都有望和正在工作。

问题是我想不通的PHP逻辑运行的每个主机名的一个foreach,然后通过每个 disk_mountpoint 的迭代中。显然,第一部分是容易的:

  $查询= $ DB-GT&;执行(选择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留下systems.id = capacity_disk.server_id,其中capacity_disk.disk_datetime = UNIX_TIMESTAMP(SUBDATE(CURRENT_DATE,3))和systems.id = 44加入capacity_disk;);的foreach($查询为$服务器){}

在这一点上,我可以通过引用主机服务器$ ['主机'] - 这又是罚款。现在的问题是 - 每个 $服务器['主机'] 项目(此ResultSet 1服务器,但2项我想也遍历),我倒要遍历两个 disk_mountpoint 的并能够操纵数据库中的表行中的数据。另外美中不足的是,我需要能够报到我的所有项目,并在格式返回的东西:

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

因此​​,这两个项目样本输出会沿着线的东西:

 /备份','1870561476','81%
'/','15126920','30%'

我从来没有在PHP这样做,有一个很难搞清楚如何把逻辑code。


解决方案

根据您的描述和样本输出它不是完全清楚为什么你需要把它当作一个嵌套数组,但由于数据库查询总是返回一个列表,你首先必须遍历结果将其转换为一个嵌套数组,如下图所示,然后可以循环通过做你所需要的。

  $ list_by_server =阵列(); //初始化数组来组织通过主机名结果
的foreach($查询作为$行){
    $ list_by_server [$行['主机'] = $行;
}的foreach($ list_by_server为$服务器){
    //任何每台服务器的设置和行动去这里
    的foreach($服务器作为$磁盘){
        //过程磁盘的详细信息和更新数据库
        //输出磁盘详细信息
    }
}

您也可以建立一个变量来跟踪当前的服务器如下图所示,避免出现两个foreach循环。

  $目前=''; //初始化跟踪车
的foreach($查询作为$行){
    如果($当前== $行['主机']){
        //做另一行的东西,从当前的主机
    }其他{
        //这是一个新的主机
        //做一些与行
        $当前== $行['主机']; //更新跟踪变种
    }
}

I'm having an issue with foreach and nested arrays in PHP. I get the whole foreach ($var as $newvar) and such, but I've come across a new requirement that I can't seem to figure out/vizualize. Here's a simple query:

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;

I get the following result:

+--------------------------------+-----------+-----------------+---------------+----------------------------+---------------+------------+-------------------+
| 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)

All this data is expected and is working.

The problem is I can't figure out the PHP logic to run a foreach for each of the hostnames and then iterated through each of the disk_mountpoint's. Obviously, the first part is easy:

$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) {

}

At this point I could reference the hostname via $server['hostname'] - which again is fine. The problem is - for each of the $server['hostname'] items (1 server in this resultset, but 2 items I'd like to also iterate through), I'd like to iterate over the two disk_mountpoint's and be able to manipulate the data in the table row in the database. The other catch is that I need to be able to report back all my items and return something in the format of:

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

So, sample output for the two items would be something along the lines of:

'/backup','1870561476','81%'
'/','15126920','30%'

I've never done this in PHP and having a hard time figuring out how to put the logic to code.

解决方案

Based on your description and sample output it's not totally clear why you need to treat this as a nested array, but since a database query always returns a single list, you'd first have to loop through the results to convert them to a nested array as shown below, and then you could loop through that to do what you need.

$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
    }
}

You could also set up a variable to track the current server as shown below and avoid two foreach loops.

$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 
    }
}

这篇关于麻烦的foreach和嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆