如何从php中的多维数组中获取特定值?

时间:2017-01-18 10:51:41

标签: php arrays multidimensional-array

如何获取此多维数组的id,friendlyname,url和status的值,如何通过数组循环获取特定值。另外,如何根据ID

获取单个监视器的类型和日期时间
Array
(
    [stat] => ok
    [offset] => 0
    [limit] => 50
    [total] => 4
    [monitors] => Array
        (
            [monitor] => Array
                (
                    [0] => Array
                        (
                            [id] => 778539790
                            [friendlyname] => Centaur
                            [url] => http://centaurdatacorp.in
                            [type] => 1
                            [subtype] => 
                            [keywordtype] => 
                            [keywordvalue] => 
                            [httpusername] => 
                            [httppassword] => 
                            [port] => 
                            [interval] => 300
                            [status] => 2
                            [alltimeuptimeratio] => 100
                            [log] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => 2
                                            [datetime] => 01/10/2017 10:16:51
                                        )

                                    [1] => Array
                                        (
                                            [type] => 98
                                            [datetime] => 01/10/2017 10:16:39
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 778539794
                            [friendlyname] => Centaur dotcom
                            [url] => http://centaurdatacorp.com
                            [type] => 1
                            [subtype] => 
                            [keywordtype] => 
                            [keywordvalue] => 
                            [httpusername] => 
                            [httppassword] => 
                            [port] => 
                            [interval] => 300
                            [status] => 2
                            [alltimeuptimeratio] => 100
                            [log] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => 2
                                            [datetime] => 01/10/2017 10:17:22
                                        )

                                    [1] => Array
                                        (
                                            [type] => 98
                                            [datetime] => 01/10/2017 10:17:10
                                        )

                                )

                        )


                )

        )

)

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要使用那个数组结构。 如果您自己创建,我建议您删除“监视器”数组,并将其内容移动到“监视器”

无论如何,让我们说你的数组名为$ monitors

foreach($monitors['monitors'] as $k => $v) { 
    foreach($v['monitor'] as $monitor) { 

         echo "monitor id: " . $monitor['id'] . "<br>\n";
         echo "friendly name: " . $monitor['friendlyname'] . "<br>\n";
    }
}

获取监视器ID的日期时间:

function getLogById($monitors, $id) {
    foreach($monitors['monitors'] as $k => $v) { 
        foreach($v['monitor'] as $monitor) { 
            if($monitor['id'] == $id) {
                //Depending on what you exactly want returned, do something here
                return $monitor['log'];
            }
        }
    }

    return null;
}

//and you can use this like so:
getLogById($monitors, 778539794);