【问题标题】:PHP - getting value from elasticsearch jsonPHP - 从 elasticsearch json 中获取价值
【发布时间】:2019-07-20 12:18:44
【问题描述】:

我有以下来自 elasticsearch 的 json:

{"took":0,"timed_out":false,"_shards":{"total":6,"successful":6,"skipped":0,"failed":0},"hits":{"total":17441,"max_score":0.0,"hits":[]},"aggregations":{"unique":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"6b14cd11183d.mynetwork","doc_count":7336},{"key":"baa98a5a158a.mynetwork","doc_count":7336},{"key":"4d5512331b4f.mynetwork","doc_count":2444},{"key":"e22d4da1d2c8.mynetwork","doc_count":139},{"key":"dc740b47a576.mynetwork","doc_count":133},{"key":"0b4f83dcc65b.mynetwork","doc_count":46},{"key":"172.11.0.5","doc_count":1}]}}}

我尝试获取存储桶中的所有密钥,但我不知道如何获取。 这是json_decode:

Array ( [took] => 1 [timed_out] => [_shards] => Array ( [total] => 6 [successful] => 6 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => 17441 [max_score] => 0 [hits] => Array ( ) ) [aggregations] => Array ( [unique] => Array ( [doc_count_error_upper_bound] => 0 [sum_other_doc_count] => 0 [buckets] => Array ( [0] => Array ( [key] => 6b14cd1f583d.mynetwork [doc_count] => 7336 ) [1] => Array ( [key] => baa98a5a258a.mynetwork [doc_count] => 7336 ) [2] => Array ( [key] => 4d5512331b4f.mynetwork [doc_count] => 2444 ) [3] => Array ( [key] => e22d4da114c8.mynetwork [doc_count] => 139 ) [4] => Array ( [key] => dc740b471076.mynetwork [doc_count] => 133 ) [5] => Array ( [key] => 0b4f83dc145b.mynetwork [doc_count] => 46 ) [6] => Array ( [key] => 172.19.0.5 [doc_count] => 1 ) ) ) ) ) 

打印密钥的正确方法是什么?

【问题讨论】:

    标签: php arrays json elasticsearch


    【解决方案1】:

    您可以使用简单的 for 循环,但更酷的解决方案是使用 array_column 函数,如下所示:

    <?php
    
        $json = '{"took":0,"timed_out":false,"_shards":{"total":6,"successful":6,"skipped":0,"failed":0},"hits":{"total":17441,"max_score":0.0,"hits":[]},"aggregations":{"unique":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"6b14cd11183d.mynetwork","doc_count":7336},{"key":"baa98a5a158a.mynetwork","doc_count":7336},{"key":"4d5512331b4f.mynetwork","doc_count":2444},{"key":"e22d4da1d2c8.mynetwork","doc_count":139},{"key":"dc740b47a576.mynetwork","doc_count":133},{"key":"0b4f83dcc65b.mynetwork","doc_count":46},{"key":"172.11.0.5","doc_count":1}]}}}';
    
        $json = json_decode($json, 1);
    
        // All you need to do!
        $json = array_column($json['aggregations']['unique']['buckets'], 'key');
    
        var_dump($json);
    

    测试它here

    【讨论】:

      【解决方案2】:
      $json = '{"took":0,"timed_out":false,"_shards":{"total":6,"successful":6,"skipped":0,"failed":0},"hits":{"total":17441,"max_score":0.0,"hits":[]},"aggregations":{"unique":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"6b14cd11183d.mynetwork","doc_count":7336},{"key":"baa98a5a158a.mynetwork","doc_count":7336},{"key":"4d5512331b4f.mynetwork","doc_count":2444},{"key":"e22d4da1d2c8.mynetwork","doc_count":139},{"key":"dc740b47a576.mynetwork","doc_count":133},{"key":"0b4f83dcc65b.mynetwork","doc_count":46},{"key":"172.11.0.5","doc_count":1}]}}}';
      
      $json_decoded = json_decode($json, true);
      $bucket_keys = [];
      foreach($json_decoded['aggregations']['unique']['buckets'] as $bucket) {
          $bucket_keys[] = $bucket['key'];
      }
      
      print_r($bucket_keys);
      

      http://sandbox.onlinephpfunctions.com/code/7c7acbcbaee3c0092aa66d26b560ae03c66c3637

      【讨论】:

        猜你喜欢
        • 2013-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-31
        • 1970-01-01
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多