json解码成阵列

时间:2012-11-25 05:36:43

标签: php json

我正在尝试将标题存储在数组中,并从下面的网址中提取长cm连续字符串。

http://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:1980_births&format=json

我目前的代码:

$url = 'http://en.wikipedia.org/w/api.php?
action=query&list=categorymembers&cmtitle=Category:'.$cat.'&format=json';

$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "asdf");

$c = curl_exec($ch);
$json = json_decode($c);

$array = $json->{'query'}->{'categorymembers'}->{'title'};

1 个答案:

答案 0 :(得分:5)

尝试添加json_decode的第二个参数,例如:

$json = json_decode($c, true);

获取cmcontinue值为:

echo $json["query-continue"]["categorymembers"]["cmcontinue"];

titles

$titles = array();
foreach($json["query"]["categorymembers"] as $vals) {
    array_push($titles, $vals["title"]);
}

echo "<pre>"; print_r($titles);