PHP循环多暗阵列

时间:2016-12-10 00:52:02

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

                        )

                )

        )

)

我需要从responsetime数组中获取datetime和value的值。我尝试了以下但似乎没有返回任何东西。

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

$ multidim是上面列出的大型多维数组。任何帮助表示赞赏,因为我不知道从哪里开始。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

访问你应该做的所有响应时间......就像这样

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,...
}

即如果您只想访问第一台显示器的所有响应时间。如果你想访问所有监视器及其响应时间,你需要2个循环,例如

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,...
    }
}

我希望能让你朝着正确的方向前进:)