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

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?