Festim Dehar
Festim Dehar

Reputation: 117

Get specific object from specific Id JSON

I have a little problem with my json file, what I am trying to do is get the specific object from my json file, so in this json above when i add this parameter stream_id= and for example i add this id stream_id=200 it should show only the object with that id, so to be more clear it should show id:200, name:Ravi Tamada, email:[email protected] etc etc ,with PHP, thanks

{
        "contacts": [
            {
                    "id": "200",
                    "name": "Ravi Tamada",
                    "email": "[email protected]",
                    "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": "[email protected]",
                    "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": "[email protected]",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",


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

Upvotes: 4

Views: 4923

Answers (4)

Nicolas
Nicolas

Reputation: 8670

You could iterate through your JSON and look for the Id you need. Simple as that.

$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;
        }
    }
}

Upvotes: 3

Fanky
Fanky

Reputation: 1785

You may need to get the data by id more often and not loop through the array every time. So you can

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

and then access:

echo $users[202]->email;

Upvotes: 0

Mark
Mark

Reputation: 4883

You should be able to loop through the JSON with a simple js/jquery function like below, where you just pass the specified id.

function getJsonById(id){
var json = {
        "contacts": [
            {
                    "id": "200",
                    "name": "Ravi Tamada",
                    "email": "[email protected]",
                    "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": "[email protected]",
                    "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": "[email protected]",
                    "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 "";
});

Upvotes: 0

user5911134
user5911134

Reputation:

May be you can use json_decode with $assoc=true (this option converts the json to an array, even to nested arrays) and then loop through the array to find the desired element.

Upvotes: 0

Related Questions