嗨我有一个json输出如下:
. and so on
.
.
[vote_count] => 76
[alternative_titles] => stdClass Object
(
[titles] => Array
(
[0] => stdClass Object
(
[iso_3166_1] => US
[title] => Basic Math
)
[1] => stdClass Object
(
[iso_3166_1] => CN
[title] => 爱爱上云端
)
)
)
)
如果检测到“iso_3166_1” 中的“US”,我如何获得 “标题”值?
我尝试自己解决它,但我的代码不起作用,这是我的代码:
<?php
$json_tmdb = 'http://api.themoviedb.org/3/movie/tt1956620?api_key=31d2bb992c7bdcbfb98b554ccab12b88&append_to_response=alternative_titles';
$data = json_decode(file_get_contents($json_tmdb));
if (in_array('US', $json_tmdb)) {
echo "Match found";
// echo title value
}
else
{
echo "Match not found";
return false;
}
?>
希望有人在这里可以帮助我,thx q
答案 0 :(得分:1)
试试这个:
function findKey($array,$k,$v)
{
foreach($array as $key=>$value)
{
if(is_array($value))
{
findKey($value,$k,$v);
}
else
{
if($key == $k && $v == $value){
echo "key = $k value = $value\n";
}
}
}
}
$json_tmdb = 'http://api.themoviedb.org/3/movie/tt1956620?api_key=31d2bb992c7bdcbfb98b554ccab12b88&append_to_response=alternative_titles';
findKey(json_decode(file_get_contents($json_tmdb),true),'iso_3166_1','US');