douji6735 2018-06-11 16:23
浏览 49
已采纳

PHP搜索JSON而不循环

I have a large JSON array which is the result of querying the API of an Icinga2 monitoring system.

I have used json_decode like this in my code to decode it:

$response = json_decode($array, true);

and I can see the output looks like this:

        Array
            (
                [results] => Array
                    (
                        [0] => Array
                            (
                                [attrs] => Array
                                    (
                                        [__name] => HOSTNAME0
                                        [acknowledgement] => 0
                                        [acknowledgement_expiry] => 0
                                        ...
                                        ...
                                        [state] => 0
                                        [state_type] => 1
                                 [meta] => Array
                                     (
                                     )

                                 [name] => HOSTNAME0
                                 [type] => Host
            )


                        [1] => Array
                            (
                                [attrs] => Array
                                    (
                                        [__name] => HOSTNAME1
                                        [acknowledgement] => 0
                                        [acknowledgement_expiry] => 0
                                        ...
                                        ...
                                        [state] => 0
                                        [state_type] => 1   
                                 [meta] => Array
                                     (
                                     )

                                 [name] => HOSTNAME1
                                 [type] => Host
            )

There are 400 Records in total and it's quite a complex structure but the only bits I am really interested in are the name and state fields. Basically my script has a list of 150 hostnames from another source and what I want to do is for each hostname, search for it in the array and return the value of the state field for that host. So far I've been struggling to do this without looping through the entire array for each of the 150 hostnames. There must be a more efficient way to do a lookup in the array based on a hostname and return a single value but I can't figure it out.

  • 写回答

2条回答 默认 最新

  • douxia9826 2018-06-11 16:34
    关注

    Given, the name field has no logical sorting inside the json result, there is no way to look at least once at each element. If they are sorted alphabetical, you could use a simple binary search, which would give you the result in O(log(n)).

    The other thing is, if you have to search for multiple names, you could put them inside an name assiciated array. This way, you only have an initial overhead of O(n) building the list and each following search would return you the state on O(1).

    // building the array
    $states = [];
    foreach ($items as $item) {
        $states[$item['name']] = $item['state'];
    }
    
    looking for HOSTNAME1
    $state = $states['HOSTNAME1'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应