Reddit JSON и открытие foreach () и получение значений

Я пытаюсь прочитать Reddit JSON, используя мой аккаунт в качестве примера.

Пробовал решение выше как:

$string_reddit = file_get_contents("https://www.reddit.com/user/joshfolgado/about.json");

$json = json_decode($string_reddit, true);

$children = $json['data'];
foreach ($children as $child){

$linkkarma = $child['data']['link_karma'];

}

Также попробовал:

foreach ($json->data as $mydata){

$values["Latest_Karma"] = $mydata['link_karma'];

}

Также попробовал:

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: reddiant api script\r\n"));

$context = stream_context_create($opts);
$url = "http://www.reddit.com/user/joshfolgado/about.json";
$json = file_get_contents($url, false, $context);

$result = json_decode($json, true);

foreach ($result as $child){
$values['Latest_Karma'] = $child['data']['link_karma'];
}

Потратил пару часов, пытаясь получить значения для любого из элементов в массиве «data», так и не смог их получить.

Что я делаю неправильно? Что мне не хватает?

Любая помощь приветствуется.

Спасибо

0

Решение

Небольшая модификация поста pee2pee (для него он вернул неопределенную ошибку индекса)

$string_reddit = file_get_contents("http://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);

$children = $json['data'];
$user = [];

foreach ($children as $key => $value)
{
$user[$key] = $value;
}

echo $user['name']; //Now you can use the $user array to access all the properties!

Это работает для меня ->

введите описание изображения здесь

0

Другие решения

$string_reddit = file_get_contents("http://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);

$children = $json['data'];
foreach ($children as $child){
$link_karma= $child['link_karma'];
}
1

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector