我有这个JSON对象,包含时间戳和单词以及使用Amazon Transcribe制作的转录的其他元素:
[
{
"start_time":"29.420",
"end_time":"40.500",
"alternatives":[
{
"confidence":"0.4332",
"content":"Foo"
}
],
"type":"pronunciation"
},
{
"start_time":"156.087",
"end_time":"156.567",
"alternatives":[
{
"confidence":"0.8786",
"content":"Hello"
}
],
"type":"pronunciation"
},
{
"start_time":"156.597",
"end_time":"157.737",
"alternatives":[
{
"confidence":"0.9439",
"content":"how"
}
],
"type":"pronunciation"
},
{
"start_time":"157.737",
"end_time":"157.917",
"alternatives":[
{
"confidence":"0.9916",
"content":"are"
}
],
"type":"pronunciation"
},
{
"start_time":"157.917",
"end_time":"158.287",
"alternatives":[
{
"confidence":"0.6898",
"content":"you"
}
],
"type":"pronunciation"
},
{
"start_time":"158.717",
"end_time":"160.637",
"alternatives":[
{
"confidence":"0.6357",
"content":"Bar"
}
],
"type":"pronunciation"
}
]
我需要在start_time
= 100和end_time
= 160之间返回内容:
你好,你好吗
我尝试使用json_decode
将其转换为数组,然后使用ksort
,implode
,slice
,等进行操作,但未成功。
答案 0 :(得分:2)
json_decode()
获取数组的JSON,然后循环遍历记录,只连接(或存储或任何你需要的)$start_date > 100 && $end_time < 160
。
<?php
$data = json_decode($json, true);
$endResult = "";
foreach ($data as $res) {
if ($res["start_time"] > 100 && $res["end_time"] < 160) {
$endResult .= $res["alternatives"][0]["content"]." ";
}
}
var_dump($endResult); // Hello how are you
或者您可以array_filter()
具有相同条件的数组并获得仅包含您需要的元素的数组,以便以后根据需要进一步处理:
<?php
$data = json_decode($json, true);
$endResult = array_filter($data, function ($el) {
return $el["start_time"] > 100 && $el["end_time"] < 160;
});
var_dump($endResult); // only elements within that time
答案 1 :(得分:0)
也许尝试这样的事情?而不是第二次进入foreach循环,你可以使用数组来满足你的需求。
$array = json_decode($json);
$data = [];
foreach($array as $el) {
$object = (object) $el;
$start = $object->start_time;
$stop = $object->end_time;
$content = $object->alternatives[0]->content;
$data[] = (array) [
'start' => (float) $start,
'stop' => (float) $stop,
'content' => (string) $content,
];
}
$between = [100,160];
foreach($data as $el) {
if($el['start'] >= $between[0] && $el['stop'] <= $between[1]) {
echo $el['content'] . ' ';
}
}
答案 2 :(得分:0)
您可以使用json_decode
函数将JSON编码的字符串$json
转换为PHP变量。然后,您将能够迭代它并应用条件来构建您的内容:
<?php
$json = '[
{
"start_time":"29.420",
"end_time":"40.500",
"alternatives":[
{
"confidence":"0.4332",
"content":"Foo"
}
],
"type":"pronunciation"
},
{
"start_time":"156.087",
"end_time":"156.567",
"alternatives":[
{
"confidence":"0.8786",
"content":"Hello"
}
],
"type":"pronunciation"
},
{
"start_time":"156.597",
"end_time":"157.737",
"alternatives":[
{
"confidence":"0.9439",
"content":"how"
}
],
"type":"pronunciation"
},
{
"start_time":"157.737",
"end_time":"157.917",
"alternatives":[
{
"confidence":"0.9916",
"content":"are"
}
],
"type":"pronunciation"
},
{
"start_time":"157.917",
"end_time":"158.287",
"alternatives":[
{
"confidence":"0.6898",
"content":"you"
}
],
"type":"pronunciation"
},
{
"start_time":"158.717",
"end_time":"160.637",
"alternatives":[
{
"confidence":"0.6357",
"content":"Bar"
}
],
"type":"pronunciation"
}
]';
$json = json_decode($json, true);
$content = "";
foreach ($json as $item) {
if ($item["start_time"] >= 100 && $item["end_time"] <= 160) {
$content .= $item["alternatives"][0]["content"] . " ";
}
}
echo trim($content);