1
$json = file_get_contents('https://api.twitch.tv/kraken/streams');
$jsonDecoded = json_decode($json);

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($jsonDecoded));
fclose($fp);

我只需要 2 个字段https://api.twitch.tv

它是流->频道->名称和流->观众;

我怎样才能为每个建筑创造好处?

4

2 回答 2

0

您可以简单地对其进行迭代并像这样复制:

$streams = array();
foreach ($jsonDecoded->streams as $stream) {
    $streams[$stream->channel->display_name] = $stream->viewers;
}

然后你可以像其他key=>value php数组一样使用它,即:

foreach ($streams as $stream => $viewers) {
    echo $stream.": ".$viewers."\n";
}
于 2013-02-20T08:28:30.993 回答
0
foreach ($jsonDecoded->streams as $stream) {
    $streams[$stream->channel->display_name] = $stream->viewers;
}

这对我不起作用。我必须将其更改为此才能正常工作:

foreach ($jsonDecoded->streams as $stream) {
    $streams[$stream->channel->display_name] = $stream->viewers;
}
于 2015-08-05T19:44:44.853 回答