不是一個編碼器,我試圖做下面的事情,並放鬆我的想法試圖做到這一點。我相信答案可能只是基本的,但我似乎無法找到答案。PHP嵌套的foreach返回鍵兩次
無論如何,在這裏。 有一個多維數組:
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
當然,你不只是運行代碼兩次,它寫了兩次日誌? – deceze