Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific object from specific Id JSON

Tags:

json

php

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" 
            }
        ]
    }
like image 229
Festim Dehar Avatar asked Oct 22 '25 03:10

Festim Dehar


1 Answers

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;
        }
    }
}
like image 101
Nicolas Avatar answered Oct 23 '25 20:10

Nicolas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!