PHP-从Elasticsearch JSON获得价值



我有以下来自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 ) ) ) ) ) 

打印键的正确方法是什么?

$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/7c7acbcbaee3c0092aa66d26b560ae0ae0ae03c6666c3637

您可以使用简单的循环使用,但是冷却器的解决方案是使用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);

在这里测试。

最新更新