0
Array
(
    [stat] => ok
    [offset] => 0
    [limit] => 50
    [total] => 1
    [monitors] => Array
        (
            [monitor] => Array
                (
                    [0] => Array
                        (
                            [id] => 
                            [friendlyname] => 
                            [url] => 
                            [type] => 3
                            [subtype] => 
                            [keywordtype] => 
                            [keywordvalue] => 
                            [httpusername] => 
                            [httppassword] => 
                            [port] => 
                            [interval] => 300
                            [status] => 2
                            [alltimeuptimeratio] => 100
                            [log] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => 2
                                            [datetime] => 11/24/2016 04:01:32
                                        )

                            [responsetime] => Array
                                (
                                    [0] => Array
                                        (
                                            [datetime] => 12/09/2016 19:34:02
                                            [value] => 109
                                        )

                                    [1] => Array
                                        (
                                            [datetime] => 12/09/2016 19:29:02
                                            [value] => 110
                                        )

                                    [2] => Array
                                        (
                                            [datetime] => 12/09/2016 19:24:02
                                            [value] => 110
                                        )
                                )

                        )

                )

        )

)

I need to get the value of datetime, and value from the responsetime array. I tried the following but it seems to not return anything.

foreach($multidim as $value) {
    foreach($value as $key => $val) {
        if($key == "responsetime") {
            echo $val[3];
        }
    }
}

Where $multidim is the large multi-dim array listed above. Any help is appreciated as I am not sure where to go from here.

Thank you in advance.

Sam Marck
  • 5
  • 4
  • Datetime inside log or response time ? – georoot Dec 10 '16 at 00:53
  • You're only looking as far as `$multidim["monitors"]["monitor"]`. – miken32 Dec 10 '16 at 00:55
  • 3
    Possible duplicate of [PHP multi dimensional array search](http://stackoverflow.com/questions/6661530/php-multi-dimensional-array-search) – miken32 Dec 10 '16 at 00:55
  • 1
    @miken32 Doesn't look like the same question, they appear to be trying to search for an individual value within a multi dimensional array. While I was trying to return the entire listing with it's values. – Sam Marck Dec 10 '16 at 02:23
  • the linked duplicate target is **not** a good duplicate, as in effect, the answers there cover searching a one dimensional array, but your problem is not so far @Sam. If you know that the structure will be like that, you can iterate over the monitors directly instead of the root key. then responsetime will be an existing key. – Félix Adriyel Gagnon-Grenier Dec 10 '16 at 02:49
  • Possible duplicate of [Search for a key in an array, recursively](http://stackoverflow.com/questions/3975585/search-for-a-key-in-an-array-recursively) – Félix Adriyel Gagnon-Grenier Dec 10 '16 at 04:35

1 Answers1

0

to access all the response times you should do sth like this

foreach($multidim['monitors']['monitor'][0]['responsetime'] as $key => $value) {
    //here $key will be 0,1,2,3...
    //$value['datetime'] will be 11/24/2016 04:01:32...
    //$value['value'] will be 109,110,...
}

that is if you just want to access all the response times of the first monitor. if you want to access all the monitors and their response times you would need 2 loops for example

foreach($multidim['monitors']['monitor'] as $monitorId => $monitorData) {
    foreach($monitorData['responsetime'] as $key => $value) {
        //here you can access all the variables e.g
        //$monitorId will be 0,1,2,3...
        //$key will be 0,1,2,3...
        //$value['datetime'] will be 11/24/2016 04:01:32...
        //$value['value'] will be 109,110,...
    }
}

I hope that sends you in the right direction :)

flynorc
  • 829
  • 6
  • 13