1

不是编码员,我正在尝试做以下事情并且失去了我的想法。我确信答案可能与他们得到的一样基本,但我似乎找不到答案。

无论如何,这里去。有一个多维数组:

Array
[module 2] => Array
    (
        [1] => SimpleXMLElement Object
            (
                [0] => module 2 EARL outlet temperature
            )

        [2] => SimpleXMLElement Object
            (
                [0] => module 2 inlet temperature
            )
        [15] => SimpleXMLElement Object
            (
                [0] => module 2 EARL inlet temperature
            )

        [19] => SimpleXMLElement Object
            (
                [0] => module 2 outlet temperature
            )

    )

[module 6] => Array
    (
        [3] => SimpleXMLElement Object
            (
                [0] => module 6 EARL inlet temperature
            )

        [4] => SimpleXMLElement Object
            (
                [0] => module 6 asic-4 temperature
            )

        [11] => SimpleXMLElement Object
            (
                [0] => module 6 RP inlet temperature
            )

        [24] => SimpleXMLElement Object
            (
                [0] => module 6 asic-3 temperature
            )

        [25] => SimpleXMLElement Object
            (
                [0] => module 6 inlet temperature
            )

        [26] => SimpleXMLElement Object
            (
                [0] => module 6 EARL outlet temperature
            )

        [28] => SimpleXMLElement Object
            (
                [0] => module 6 outlet temperature
            )

        [30] => SimpleXMLElement Object
            (
                [0] => module 6 RP outlet temperature
            )

    )

我需要的是,从每个数组(模块 1、模块 2 等...)中,从每个子数组值返回数字键。基本上每个子数组键对应于另一个数组的键,该数组包含要由 rrdtool 绘制的温度。

感谢昨晚其他人的帮助,我能够按“模块#”正确地对值进行分组(我最终如何得到上面的数组)。但是现在每次我运行我的 foreach 循环(如下)时,我都会得到两次返回的结果。

## Just for testing my foreach loops
foreach ($groupedmods as $modgroupname => $sensorname) {
    foreach ($sensorname as $dsindex => $sensor) {
        if($dsindex != 0) {
        file_put_contents('/usr/local/nagiosxi/var/php.log', print_r($dsindex, true). "\n",     FILE_APPEND);
        }
    }
}
## Draw some graphs
#foreach ($groupedtemps as $modgroupname ) {
#       $ds_name[$dcnt] = "Module Temps Test";
#       $opt[$dcnt] = "--vertical-label \"Temp\" --title \"Module Temps Test \" ";
#
#       foreach ($modgroupname as $dsindex ) {
#               if($dsindex != 0) {
#file_put_contents('/usr/local/nagiosxi/var/php.log', print_r($dsindex, true ). "\n", FILE_APPEND);
#                $def[$dcnt] = "DEF:var$dsindex=$rrdfile:$DS[$dsindex]:AVERAGE " ;
#                $def[$dcnt] .= "LINE2:var$dsindex#F00808:$sensor\"\" " ;
#               }
#       } 
#}

输出我需要两次的索引列表:

1
2
15
19
3
4
11
24
25
26
28
30
5
16
17
20
21
22
23
29
6
8
7
18
9
31
10
27
12
35
13
32
14
33
34
1
2
15
19
3
4
11
24
25
26
28
30
5
16
17
20
21
22
23
29
6
8
7
18
9
31
10
27
12
35
13
32
14
33
34
4

1 回答 1

1

Not super sure what's going on with you're version of PHP (maybe it's your objects, since I'm not using those), but I use the exact same code and array structure you do and get the same result. Like deceze says, sure you didn't run the program twice? The file is being used in append mode, so a second run won't overwrite the first and will just concat onto it.

If you didn't run it twice and the code still gives you errors you can use alternate code to just play with the keys. Give this a shot, the functionality should be the same and maybe it will solve your errors (if the above doesn't):

$mod_keys = array_keys($groupedmods);
foreach ($mod_keys as $k) {
  $new_keys = array_keys($groupedmods[$k]);
  foreach ($new_keys as $key) {
    if ($key != 0) {
      file_put_contents('/usr/local/nagiosxi/var/php.log', print_r($key, true)."\n", FILE_APPEND);
    }
  }
}
于 2013-01-09T02:31:31.973 回答