1

Not being a coder, I'm trying to do the following and loosing my mind trying to do it. I'm sure the answer probably about as basic as they get but I can't seem to find an answer.

Anyways, here goes. Have a multidimensional array:

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
            )

    )

What I need is, from each array (module 1, module 2, etc...), return the numeric keys from each of the subarray values. Basically each of the subarray keys corresponds to the key of a another array which contains temperatures to be graphed by rrdtool.

Thanks to someone elses help last night I was able to correctly group the values by "module #" (how I ended up with the array above). But now every time I run through my foreach loops (below) I get the results returned twice.

## 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\"\" " ;
#               }
#       } 
#}

outputs the list of indexes I need two times:

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
Ben Shellrude
  • 79
  • 1
  • 7

1 Answers1

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);
    }
  }
}
hjc1710
  • 576
  • 4
  • 17
  • 1
    Thanks yes... looks like the Nagios code is running it twice for some reason. Glad to know I wasn't crazy and way out to lunch in terms of my understanding of the code! – Ben Shellrude Jan 09 '13 at 17:57