Nad Nad - 4 months ago 15
JSON Question

json decode array PHP

I'm trying to decode a json string, i want to get just langlinks value,
my json string is:

{
"batchcomplete": "",
"query": {
"pages": {
"105219": {
"pageid": 105219,
"ns": 0,
"title": "Cancer",
"langlinks": [
{
"lang": "ar",
"*": "\u0633\u0631\u0637\u0627\u0646"
}
]
}
}
}
}


I tried this code:

$results = json_decode($api_response, true);
$list = array();
foreach ($results['query']['pages'] as $k => $v)
{
var_dump($v);
foreach($v as $key => $val)
{
array_push($list, $val);
}
}

return $list;


But it does not accede to the value that I want, when i add

var_dump(array_key_exists('langlinks', $v));


it gives me false :/

Answer

I just tested this and this seems to return true

$source = file_get_contents('https://en.wikipedia.org/w/api.php?action=query&titles=Cancer&prop=langlinks&lllang=ar&format=json');
$results = json_decode($source, true);

foreach ($results['query']['pages'] as $k => $v)
{
    var_dump(array_key_exists('langlinks', $v));die();
}

If you are still having trouble, maybe you can post more code?

Comments