elasticsearch,Php,Arrays,Json,elasticsearch" /> elasticsearch,Php,Arrays,Json,elasticsearch" />

PHP-从elasticsearch json获取值

PHP-从elasticsearch json获取值,php,arrays,json,elasticsearch,Php,Arrays,Json,elasticsearch,我从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,"buck

我从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_解码:

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

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

<?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);