foreach只获得第一个值,而不是循环
$all =TwitchApi::streams($options);
foreach ($all['streams'] as $streams){
$stream = new streams;
$stream->stream_id = $streams['channel']['_id'];
$stream->game = $streams['channel']['game'];
$stream->service = 'twitch';
$stream->viewers =$streams['channel']['views'];
//$stream->save();
var_dump($all['streams']);
}
当我变换转储($ all [' streams'])时,我得到了这个:
{
"_total": 16042,
"streams": [
{
"_id": 26962439136,
"game": "Hearthstone",
"broadcast_platform": "live",
"community_id": "",
"community_ids": [],
"viewers": 16589,
"video_height": 1080,
"average_fps": 60,
"delay": 0,
"created_at": "2017-12-12T08:56:11Z",
"is_playlist": false,
"stream_type": "live",
"preview": {
"small": "https://static-cdn.jtvnw.net/previews-ttv/live_user_thijshs-80x45.jpg",
"medium": "https://static-cdn.jtvnw.net/previews-ttv/live_user_thijshs-320x180.jpg",
"large": "https://static-cdn.jtvnw.net/previews-ttv/live_user_thijshs-640x360.jpg",
"template": "https://static-cdn.jtvnw.net/previews-ttv/live_user_thijshs-{width}x{height}.jpg"
},
"channel": {
"mature": false,
"status": "Thijs - Ixlid OTK Malygos Druid, New Kobold Decks ALL DAY",
"broadcaster_language": "en",
"display_name": "ThijsHS",
"game": "Hearthstone",
"language": "nl",
"_id": 57025612,
"name": "thijshs",
"created_at": "2014-02-17T20:50:47.402453Z",
"updated_at": "2017-12-12T11:05:42.423142Z",
"partner": true,
"logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/thijshs-profile_image-d1855265a6df68d1-300x300.png",
"video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/7a63e8a30c5bed72-channel_offline_image-1920x1080.png",
"profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/dc9694614e363b35-profile_banner-480.png",
"profile_banner_background_color": "",
"url": "https://www.twitch.tv/thijshs",
"views": 32597794,
"followers": 418260,
"broadcaster_type": "",
"description": "Hello, i am Thijs,21 years old and a gamer from The Netherlands. I am a professional Hearthstone player. I'm currently streaming 4-5 days a week. I hope you have fun with my stream!"
}
},
{
"_id": 26962042192,
"game": "Overwatch",
"broadcast_platform": "live",
"community_id": "",
"community_ids": [],
"viewers": 13101,
"video_height": 1080,
"average_fps": 60,
"delay": 0,
"created_at": "2017-12-12T06:58:30Z",
"is_playlist": false,
"stream_type": "live",
"preview": {
"small": "https://static-cdn.jtvnw.net/previews-ttv/live_user_sodapoppin-80x45.jpg",
"medium": "https://static-cdn.jtvnw.net/previews-ttv/live_user_sodapoppin-320x180.jpg",
"large": "https://static-cdn.jtvnw.net/previews-ttv/live_user_sodapoppin-640x360.jpg",
"template": "https://static-cdn.jtvnw.net/previews-ttv/live_user_sodapoppin-{width}x{height}.jpg"
},
"channel": {
"mature": true,
"status": "Not in my closet.",
"broadcaster_language": "en",
"display_name": "sodapoppin",
"game": "Overwatch",
"language": "en",
"_id": 26301881,
"name": "sodapoppin",
"created_at": "2011-11-22T04:40:56.75883Z",
"updated_at": "2017-12-12T11:04:12.64584Z",
"partner": true,
"logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_image-10049b6200f90c14-300x300.png",
"video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-channel_offline_image-2040c6fcacec48db-1920x1080.jpeg",
"profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_banner-1c050aa5aed3558d-480.png",
"profile_banner_background_color": "",
"url": "https://www.twitch.tv/sodapoppin",
"views": 179453317,
"followers": 1657156,
"broadcaster_type": "",
"description": "Wtf do i write here? Click my stream, or i scream."
}
},
{
"_id": 26962643024,
"game": "",
"broadcast_platform": "live",
"community_id": "",
"community_ids": [],
"viewers": 12000,
"video_height": 1080,
"average_fps": 60,
"delay": 0,
"created_at": "2017-12-12T10:00:18Z",
"is_playlist": false,
"stream_type": "live",
"preview": {
"small": "https://static-cdn.jtvnw.net/previews-ttv/live_user_yogscast-80x45.jpg",
"medium": "https://static-cdn.jtvnw.net/previews-ttv/live_user_yogscast-320x180.jpg",
"large": "https://static-cdn.jtvnw.net/previews-ttv/live_user_yogscast-640x360.jpg",
"template": "https://static-cdn.jtvnw.net/previews-ttv/live_user_yogscast-{width}x{height}.jpg"
},
"channel": {
"mature": false,
"status": "JINGLE JAM 2017 DAY 12! - OFFICE CAM",
"broadcaster_language": "en",
"display_name": "Yogscast",
"game": "",
"language": "en",
"_id": 20786541,
"name": "yogscast",
"created_at": "2011-02-27T20:19:01.423136Z",
"updated_at": "2017-12-12T11:03:58.616948Z",
"partner": true,
"logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/db623728431d29c3-profile_image-300x300.png",
"video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/35edb9a8141ab1c3-channel_offline_image-1920x1080.png",
"profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/yogscast-profile_banner-61f0e8b351336524-480.png",
"profile_banner_background_color": "#d7d7d7",
"url": "https://www.twitch.tv/yogscast",
"views": 69681161,
"followers": 661492,
"broadcaster_type": "",
"description": "Hello! And welcome to The Yogscast! Streaming every day at 5PM BST! Minecraft and multiplayer comedy gaming with a drunken dwarf, a handsome spaceman and the rest of the Yogscast family!"
}
}
但是当我的var_dump($ streams)我只得到这样的第一个值:
{
"_id": 26962440352,
"game": "Grand Theft Auto V",
"broadcast_platform": "live",
"community_id": "2b86280e-e3fd-4b7c-b1e1-ee8b2ed2818f",
"community_ids": [
"2b86280e-e3fd-4b7c-b1e1-ee8b2ed2818f"
],
"viewers": 998,
"video_height": 720,
"average_fps": 60.0282485876,
"delay": 0,
"created_at": "2017-12-12T08:56:41Z",
"is_playlist": false,
"stream_type": "live",
"preview": {
"small": "https://static-cdn.jtvnw.net/previews-ttv/live_user_alexclick-80x45.jpg",
"medium": "https://static-cdn.jtvnw.net/previews-ttv/live_user_alexclick-320x180.jpg",
"large": "https://static-cdn.jtvnw.net/previews-ttv/live_user_alexclick-640x360.jpg",
"template": "https://static-cdn.jtvnw.net/previews-ttv/live_user_alexclick-{width}x{height}.jpg"
},
"channel": {
"mature": false,
"status": "[Faily V] Alex Murphy #46",
"broadcaster_language": "fr",
"display_name": "Alexclick",
"game": "Grand Theft Auto V",
"language": "fr",
"_id": 89284114,
"name": "alexclick",
"created_at": "2015-04-23T11:39:25.363072Z",
"updated_at": "2017-12-12T11:33:48.955085Z",
"partner": true,
"logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/c9ab63d5f7e93091-profile_image-300x300.png",
"video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/bfad7c61a33b3644-channel_offline_image-1920x1080.png",
"profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/72bfa9debe329e2f-profile_banner-480.png",
"profile_banner_background_color": "",
"url": "https://www.twitch.tv/alexclick",
"views": 1920535,
"followers": 49971,
"broadcaster_type": "",
"description": "Streamer & Youtubeur mais surtout triple champion du monde de deltaplane de salon. Toutes les infos et stream sur http://www.alexclicktv.fr"
}
}
在这个循环后我有一个返回但它没有任何区别,因为我在到达return语句之前转储数据 什么错了?
答案 0 :(得分:0)
您在foreach循环$streams
中命名了第二个变量,而在循环体中引用了$stream
。
答案 1 :(得分:0)
试试这个;
$all =TwitchApi::streams($options);
$all = json_encode($all);
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
$depth = 512;
$options = 1;
$result = json_decode ($json, $assoc, $depth, $options);
$result = objectToArray($result);
var_dump($result["_total"]);
首先将all转换为JSON,然后将其转换为Array,然后从数组中获取值。
答案 2 :(得分:0)
这里是答案人:)
$all =TwitchApi::streams($options);
$clean[] = $all['streams'];
//dd($clean['0']['1']['game']);
for ($idx = 0; $idx < 100; $idx++){
//print_r($idx);
$stream = new streams;
$stream->stream_id = $clean['0'][$idx]['channel']['_id'];
$stream->game = $clean['0'][$idx]['channel']['game'];
$stream->service = 'twitch';
$stream->viewers =$clean['0'][$idx]['channel']['views'];
$stream->save();
}