arrogantprick
arrogantprick

Reputation: 163

json decode into array

I'm trying to store the titles in an array and extract the long cmcontinue string from the below url.

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

my current code:

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

Upvotes: 1

Views: 2004

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

try adding second paremeter of json_decode, like:

$json = json_decode($c, true);

And get cmcontinue value as:

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

For titles:

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

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

Upvotes: 6

Related Questions