2016-04-25 70 views
5

我一直在寻找遍布interwebz的一个简单的答案,但找不到任何。所以,问题是:PHP Json查看是否存在值

我打算解码一些JSON来查看是否存在一个值;尽管如此,我认为我没有做对。我想检查appid:730的值是否存在。

这里的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 
      }, 
     ] 
    } 
} 

这就是我想要的:

$json = file_get_contents('JSON URL HERE'); 
$msgArray = json_decode($json, true); 

if (appid: 730 exists) { 
    ... 
} 

谢谢,希望我解释不够。

+0

1.在foreach中使用'$ msgArray',然后在tree下查找'games'数组。 –

+0

'json'没有回应... [无效的Json](https://3v4l.org/ld9Oa) –

回答

2

首先,你有无效的JSON。请参阅下面字符串减速中的注释(这可能是您的问题中的拼写错误)。

$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

我们只是通过游戏阵列循环并检查其appid。然后,我们只是做一些事情,然后打破循环,以防止开销。

0
json_decode() 

实际上是要返回解码的JSON。 JSON是一个字符串,解码后的JSON是一个对象数组。

您将需要遍历该对象数组来检查每个对象。

$msgArray = json_decode($json); 

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

他使用第二个参数的真标志,使其成为一个数据数组而不是数据对象。 –

+0

好的......我不认为那是他的问题。我只是试图指导他看看解码json导致的数据结构中的每个数据实体。 – jcorry

+1

好吧,如果他被其他人告知使用阵列,不要让他混淆不同的类型。 –

0

试试这个

$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

'$ value.appid'不正确,JS比PHP更多。正确的是'$ value ['appid']' –

+0

是的,我犯了错误,谢谢:-) –

0

我觉得这个解决方案相当简单:

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

这是非常方便解析和验证结果从其他Web服务来访问他们的数据之前,所以我们避免和开发时间。

我希望这是你在找什么。