2012-11-25 101 views
1

我試圖將標題存儲在數組中,並從下面的URL中提取長cmcontinue字符串。json解碼爲陣列

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'}; 
+0

什麼是你遇到的問題?等一下。你問完美的問題與你的暱稱:) – SuitedSloth

回答

4

嘗試加入的json_decode秒paremeter,如:

$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); 
+0

完美。謝謝。 – arrogantprick