从特定Id JSON获取特定对象

uxh89sit  于 2023-03-24  发布在  其他
关注(0)|答案(4)|浏览(182)

我的json文件有一个小问题,我想做的是从我的json文件中获取特定的对象,所以在上面的这个json中,当我添加这个参数stream_id=并且例如我添加这个idstream_id=200它应该只显示具有该id的对象,所以为了更清楚,它应该显示id:200,name:Ravi Tamada,email:ravi@gmail.cometc,with 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" 
            }
        ]
    }
dwbf0jvd

dwbf0jvd1#

你可以遍历你的JSON并寻找你需要的ID,就这么简单。

$json = /* your json */;

$array = json_decode($json, true);

$result = getInfo(200, $array);

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

myss37ts2#

也许你可以使用json_decode和$assoc=true(这个选项将JSON转换为数组,甚至是嵌套数组),然后循环遍历数组以找到所需的元素。

iyzzxitl

iyzzxitl3#

您应该能够使用如下所示的简单js/jquery函数循环JSON,其中您只需传递指定的id。

function getJsonById(id){
var json = {
        "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" 
            }
        ]
    };

var l= json['contacts'].length;
var c = 0;
while(c<l){
  if(json['contacts'][c]['id']==id){
    console.log(json['contacts'][c]);
     return json['contacts'][c];
  } 
  c++;
}
}
return "";
});
f3temu5u

f3temu5u4#

您可能需要更频繁地通过id获取数据,而不是每次都循环遍历数组。

foreach($users_array as $user_object){
    $users[$user_object->id]=$user_object;
}

然后访问:

echo $users[202]->email;

相关问题