dpp3047 2016-12-10 00:52
浏览 18
已采纳

PHP循环多暗阵列

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.

  • 写回答

1条回答 默认 最新

  • dongxiane0395 2016-12-10 02:02
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?