我正在尝试重新组合/推送一个名为$new_datas
分配给一个 json 文件的数组,并$old_datas
分配给另一个 json 文件。
我试图合并这两者,但由于外部 Api 上的每个请求的拉/拉限制为 100 个项目,有时当我使用此 Api 的偏移量时,最终的 json 文件中存在一些重复的内容。我尝试在推送 $new_datas 之前取消设置该值,但目前重复的内容已经存在。
做这个的最好方式是什么?我只是一个爱好者,目前不是专业人士,所以我一个月前才开始学习。在此先感谢您的帮助
有我的代码
//Offset twitch
$next_offset = get_option('mmob_next_offset');
//First run
if(!$next_offset)
{
$next_offset = '0';
}
$query_api_link_twitch = 'http://api.justin.tv/api/stream/list.json?limit=99&category=gaming&offset='.$next_offset.''; // /!\ json
//Get all contents
$datas_get_twitch = file_get_contents($query_api_link_twitch);
if(!empty($datas_get_twitch))
{
$new_datas_twitch = json_decode($datas_get_twitch);
$old_datas_twitch = json_decode(file_get_contents($cache_target . 'twitch.json'));
$olds = array();
//I have tried this function before the array_merge but don't work
//I try to compare the 2nd value from the key "name" because it's the only one "unique"
foreach($old_datas_twitch as $key => $value)
{
//var_dump($value);
$olds[] = $value->name;
//var_dump($name);
foreach($new_datas_twitch as $k => $v)
{
$new = $v->name;
if(in_array($new, $olds))
{
unset($old_datas_twitch->$key);
}
}
}
//Merge, put, close
$twitch_new_build = array_merge((array)$old_datas_twitch, (array)$new_datas_twitch);
$datas_push_twitch = file_put_contents($cache_target . 'twitch.json', json_encode($twitch_new_build));
update_option('mmob_next_offset', $next_offset + 100);
if(!file_exists($cache_target . 'twitch.json') || !is_writable($cache_target . 'twitch.json'))
{
$email = false;
$last_known_day = (int)date('d', get_option('mmob_email_date_twitch'));
$current_day = (int)date('d', time());
//Cron run every 2 minutes...
if($current_day > $last_known_day)
{
$email = true;
}
update_option('mmob_email_date_twitch', time());
}
}
else
{
update_option('mmob_next_offset', '0');
}
有 var_dump
object(stdClass)[2533]
public 'embed_count' => string '0' (length=1)
public 'name' => string 'live_user_lunaticoldschool' (length=26)
public 'stream_count' => string '1' (length=1)
public 'subcategory' => string 'strategy' (length=8)
public 'category' => string 'gaming' (length=6)
public 'format' => string 'live' (length=4)
public 'channel_count' => int 1
public 'title' => string '"LuNaTiC Teh Macro Zerg" HD Streaming StarCraft 2 EU-Server playing Zerg [Season 4 - 2012 @ Gold League, Some Fun Random Team Games Also] ' (length=139)
public 'featured' => boolean true
public 'site_count' => string '1' (length=1)
public 'abuse_reported' => boolean false
public 'channel' =>
object(stdClass)[2534]
public 'embed_enabled' => boolean true
public 'language' => string 'en' (length=2)
public 'views_count' => string '771554' (length=6)
public 'embed_code' => string ' <object type="application/x-shockwave-flash" height="295" width="353" id="live_embed_player_flash" data="http://www.justin.tv/widgets/live_embed_player.swf?channel=lunaticoldschool" bgcolor="#000000"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://www.justin.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="auto_play=false&channel=lunaticoldschool&start_volume=25" /></object>
' (length=480)
public 'screen_cap_url_small' => string 'http://static-cdn.jtvnw.net/previews/live_user_lunaticoldschool-70x53.jpg' (length=73)
public 'subcategory' => string 'strategy' (length=8)
public 'category' => string 'gaming' (length=6)
public 'status' => string '"LuNaTiC Teh Macro Zerg" HD Streaming StarCraft 2 EU-Server playing Zerg [Season 4 - 2012 @ Gold League, Some Fun Random Team Games Also] ' (length=139)
public 'image_url_huge' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-600x600.jpeg' (length=106)
public 'mature' => boolean false
public 'producer' => boolean true
public 'image_url_tiny' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-50x50.jpeg' (length=104)
public 'title' => string 'LuNaTiC Teh Macro Zerg ' (length=23)
public 'image_url_large' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-300x300.jpeg' (length=106)
public 'category_title' => string 'Gaming' (length=6)
public 'id' => int 19437762
public 'screen_cap_url_huge' => string 'http://static-cdn.jtvnw.net/previews/live_user_lunaticoldschool-630x473.jpg' (length=75)
public 'image_url_small' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-70x70.jpeg' (length=104)
public 'image_url_medium' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-150x150.jpeg' (length=106)
public 'timezone' => string 'Europe/Athens' (length=13)
public 'subcategory_title' => string 'Strategy' (length=8)
public 'screen_cap_url_large' => string 'http://static-cdn.jtvnw.net/previews/live_user_lunaticoldschool-320x240.jpg' (length=75)
public 'channel_url' => string 'http://www.justin.tv/lunaticoldschool' (length=37)
public 'tags' => null
public 'login' => string 'lunaticoldschool' (length=16)
public 'screen_cap_url_medium' => string 'http://static-cdn.jtvnw.net/previews/live_user_lunaticoldschool-150x113.jpg' (length=75)
public 'video_height' => int 720
public 'language' => string 'en' (length=2)
public 'video_bitrate' => float 309.2265625
public 'id' => string '4010553696' (length=10)
public 'meta_game' => string 'StarCraft II: Wings of Liberty' (length=30)
public 'broadcaster' => string 'fme' (length=3)
public 'broadcast_part' => int 1
public 'audio_codec' => string 'aac' (length=3)
public 'up_time' => string 'Fri Oct 19 06:18:15 2012' (length=24)
public 'video_width' => int 1280
public 'geo' => string 'GR' (length=2)
public 'channel_view_count' => int 771541
public 'channel_subscription' => boolean false
public 'embed_enabled' => boolean true
public 'stream_type' => string 'live' (length=4)
public 'video_codec' => string 'AVC' (length=3)