PHP Json sees if value exists

I look around interwebz for a simple answer, but cannot find. So the question is:

I am going to decrypt some JSON to find out if a value exists; although, I don’t think I am doing it right. I want to check if appid value exists: 730.

Here's the JSON:

{ response: { game_count: 106, games: [ { appid: 10, playtime_forever: 67 }, { appid: 730, playtime_forever: 0 }, { appid: 368900, playtime_forever: 0 }, { appid: 370190, playtime_forever: 0 }, ] } } 

This is what I want:

 $json = file_get_contents('JSON URL HERE'); $msgArray = json_decode($json, true); if (appid: 730 exists) { ... } 

Thanks, I hope I have explained enough.

+5
source share
4 answers

First, you have invalid json. See Comment in line slowdown below (it could be a typo in your question).

 $json = '{ "response": { "game_count": 106, "games": [ { "appid": 10, "playtime_forever": 67 }, { "appid": 730, "playtime_forever": 0 }, { "appid": 368900, "playtime_forever": 0 }, { "appid": 370190, "playtime_forever": 0 } // <------ note the lack of `,` ] } }'; $arr = json_decode($json, true); foreach($arr['response']['games'] as $game) { if($game['appid'] === 730) { // I would strictly check (type) incase of 0 echo "exists"; // or do something else break; // break out if you dont care about the rest } } 

example

We just appid over the array of games and check its appid . Then we just do something and then break the loop to prevent overhead.

+2
source
 json_decode() 

actually going to return decoded JSON. JSON is a string decoded by JSON is an array of objects.

You will need to iterate over this array of objects to check each of them.

 $msgArray = json_decode($json); foreach($msgArray->response->games as $game) { if($game->appid == 730) { // it exists } } 
0
source

try it

 $json = file_get_contents('JSON URL HERE'); $msgArray = json_decode($json, true); foreach($msgArray['response']['games'] as $key => $value){ if ($value['appid'] == 730) { //do something }else{ // do else } } 
0
source

I find this solution quite simple:

 $r='{ "response": { "game_count": 106, "games": [ { "appid": 10, "playtime_forever": 67 }, { "appid": 730, "playtime_forever": 0 }, { "appid": 368900, "playtime_forever": 0 }, { "appid": 370190, "playtime_forever": 0 } ] } }'; function find($arr, $id) { if(!empty($arr)) foreach ($arr as $key => $value) { if( isset( $value->appid ) && $value->appid == $id ) return true; } return false; } $obj = json_decode($r); if( isset($obj) && isset($obj->response) ) { if( isset($obj->response->games) && !empty($obj->response->games) ) $arr = $obj->response->games; else $arr = array(); } else { echo "NOT valid found\n"; } $appid = 730; if( find($arr, $appid) ) echo "Appid $appid found\n"; else echo "Appid $appid NOT found\n"; 

it’s very convenient to analyze and verify the results coming from other web services, before accessing their data, so we also avoid development time.

Hope this is what you are looking for.

0
source

Source: https://habr.com/ru/post/1247862/


All Articles