gpt4 book ai didi

php - 使用PHP正则表达式计算ElasticSearch的结果

转载 作者:行者123 更新时间:2023-12-02 22:17:42 24 4
gpt4 key购买 nike

当ElasticSearch返回以下行时,我想保存计数结果:

{"took":13,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2242,"max_score":1.0,"hits":[{"_index":"

我希望将总结果保存到变量中,但是我找不到保存“2242”的良好正则表达式规则。

最佳答案

为什么您认为需要正则表达式?只需使用 json_decode 并像这样访问它;对于此示例,关闭了JSON:

$raw_json = '{"took":13,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2242,"max_score":1.0,"hits":[{"_index":"999999"}]}}';

$decoded_json = json_decode($raw_json);

echo '<pre>';
print_r($decoded_json);
echo '</pre>';

输出将是这样的:
stdClass Object
(
[took] => 13
[timed_out] =>
[_shards] => stdClass Object
(
[total] => 5
[successful] => 5
[failed] => 0
)

[hits] => stdClass Object
(
[total] => 2242
[max_score] => 1
[hits] => Array
(
[0] => stdClass Object
(
[_index] => 999999
)

)

)

)

然后知道您只是像这样访问 total下的 hits:
$hits_total = $decoded_json->hits->total;

echo $hits_total;

或者,如果您不容易解析对象,只需将 json_decode的第二个参数设置为 true即可返回数组。
$decoded_json = json_decode($raw_json, true);

然后像这样访问它:
$hits_total = $decoded_json['hits']['total'];

echo $hits_total;

关于php - 使用PHP正则表达式计算ElasticSearch的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23982811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com