gpt4 book ai didi

php - 从特定 Id JSON 获取特定对象

转载 作者:行者123 更新时间:2023-12-05 05:21:47 25 4
gpt4 key购买 nike

我的 json 文件有点问题,我想做的是从我的 json 文件中获取特定对象,所以在上面的这个 json 中,当我添加此参数时 stream_id= 并为例如我添加这个 id stream_id=200 它应该只显示具有该 id 的对象,所以为了更清楚它应该显示 id:200, name:Ravi Tamada, email:ravi@gmail .com 等等,用 PHP,谢谢

{
"contacts": [
{
"id": "200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"url": "http://149.202.196.143:8000/live/djemal/djemal/592.ts"
},
{
"id": "201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts"
},
{
"id": "202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",


"url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts"
}
]
}

最佳答案

您可以遍历 JSON 并查找所需的 ID。就这么简单。

<?php
$json = /* your json */;
$array = json_decode($json, true);

$result = getInfo(200, $array);

function getInfo($id, $array) {
foreach($array AS $index=>$json) {
if($json['id'] == $id) {
return $json;
}
}
}

关于php - 从特定 Id JSON 获取特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233076/

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