9

I've been looking all over the interwebz for a simple answer but can't find any. So, the question is:

I'm going to decode some JSON to see if a value exists; though, I don't think I'm doing it right. I want to check if the value of appid: 730 exists.

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, hope I explained enough.

2
  • 1. Use $msgArray in a foreach, and look down the tree for games array. Commented Apr 25, 2016 at 11:34
  • the json is not responding... Invalid Json Commented Apr 25, 2016 at 11:40

4 Answers 4

5

Firstly, you have invalid json. See the comment in the string deceleration below (this might 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're just looping through the games array and checking its appid. Then we just do something and then break the loop to prevent overhead.

Sign up to request clarification or add additional context in comments.

Comments

1

try this

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

1 Comment

$value.appid is incorrect, and more for JS than PHP. Correct would be $value['appid']
1

I find this solution quite straight forward:

$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 is very convenient to parse and validate the results coming from other Web Services before accessing their data, so we avoid and development time.

I hope it is what you are looking for.

Comments

0
json_decode()

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

You will need to iterate over that array of objects to inspect each.

$msgArray = json_decode($json);

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

3 Comments

he's using the true flag for the second parameter, making it an array of data as opposed to an object of data.
ok...I don't think that's his problem though. I'm just trying to direct him toward looking at each data entity in the data structure that results from decoding the json.
Well, just don't want him to get confused on the different types if he's been told by someone else to use Arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.