Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中重新组合两个json文件并过滤新文件中的重复内容?_Php_Json_Offset_Array Merge - Fatal编程技术网

如何在php中重新组合两个json文件并过滤新文件中的重复内容?

如何在php中重新组合两个json文件并过滤新文件中的重复内容?,php,json,offset,array-merge,Php,Json,Offset,Array Merge,我正在尝试重新组合/推送一个名为$new_data的数组,该数组分配给一个json文件,而$old_data分配给另一个json文件 我曾尝试将两者合并,但对于外部Api上的每个请求,pull/for限制为100个项目,有时当我使用此Api的偏移量时,最终json文件中存在一些重复的内容。我尝试在推送$new_数据之前取消设置该值,但当前重复内容已经存在 最好的方法是什么?我只是一个爱好者,目前不是专业人士,所以我一个月前才开始学习。提前谢谢你的帮助 这是我的密码 //Offset tw

我正在尝试重新组合/推送一个名为
$new_data
的数组,该数组分配给一个json文件,而
$old_data
分配给另一个json文件

我曾尝试将两者合并,但对于外部Api上的每个请求,pull/for限制为100个项目,有时当我使用此Api的偏移量时,最终json文件中存在一些重复的内容。我尝试在推送$new_数据之前取消设置该值,但当前重复内容已经存在

最好的方法是什么?我只是一个爱好者,目前不是专业人士,所以我一个月前才开始学习。提前谢谢你的帮助

这是我的密码

    //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_垃圾场

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)
对象(stdClass)[2533]
公共“嵌入计数”=>字符串“0”(长度=1)
公共'name'=>字符串'live\u user\u lunaticoldschool'(长度=26)
公共“流计数”=>字符串“1”(长度=1)
公共“子类别”=>字符串“策略”(长度=8)
公共“类别”=>字符串“游戏”(长度=6)
公共“格式”=>字符串“活动”(长度=4)
公共“通道计数”=>int 1
公开“title”=>string“LuNaTiC Teh Macro Zerg”高清流媒体星际争霸2 EU服务器玩虫族[第4季-2012@Gold League,还有一些有趣的随机团队游戏](长度=139)
公共“特色”=>布尔值为真
公共“站点计数”=>字符串“1”(长度=1)
公共“滥用报告”=>布尔值为false
公共“频道”=>
对象(stdClass)[2534]
公共“已启用嵌入”=>布尔值true
公共“语言”=>字符串“en”(长度=2)
公共“视图数”=>字符串“771554”(长度=6)
公共“嵌入_代码”=>字符串”
'(长度=480)
公共“屏幕大小”=>字符串http://static-cdn.jtvnw.net/previews/live_user_lunaticoldschool-70x53.jpg'(长度=73)
公共“子类别”=>字符串“策略”(长度=8)
公共“类别”=>字符串“游戏”(长度=6)
公开“状态”=>字符串“疯狂的宏虫族”高清流媒体星际争霸2 EU服务器玩虫族[第4季-2012@Gold League,还有一些有趣的随机团队游戏](长度=139)
公共“图像”\u url\u巨大“=>字符串”http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-600x600.jpeg'(长度=106)
公共“成熟”=>布尔值false
公共“生产者”=>布尔值true
公共“image\u url\u tiny'=>字符串”http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-50x50.jpeg'(长度=104)
公共'title'=>字符串'LuNaTiC Teh Macro Zerg'(长度=23)
公共“image\u url\u large'=>字符串”http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-300x300.jpeg'(长度=106)
公共“类别标题”=>字符串“游戏”(长度=6)
公共“id”=>INT19437762
公共“屏幕\u上限\u url\u巨大”=>字符串”http://static-cdn.jtvnw.net/previews/live_user_lunaticoldschool-630x473.jpg'(长度=75)
公共“image\u url\u small'=>字符串”http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-70x70.jpeg'(长度=104)
公共“图像\u url\u媒体”=>字符串http://static-cdn.jtvnw.net/jtv_user_pictures/lunaticoldschool-profile_image-c283f71a8088b0a3-150x150.jpeg'(长度=106)
公共“时区”=>字符串“欧洲/雅典”(长度=13)
公共“子类别\标题”=>字符串“策略”(长度=8)
公共“屏幕大小”=>字符串”http://static-cdn.jtvnw.net/previews/live_user_lunaticoldschool-320x240.jpg'(长度=75)
公共“频道url”=>字符串”http://www.justin.tv/lunaticoldschool'(长度=37)
公共“标记”=>null
公共“登录”=>字符串“lunaticoldschool”(长度=16)
公共“屏幕\u cap\u url\u媒体”=>字符串http://static-cdn.jtvnw.net/previews/live_user_lunaticoldschool-150x113.jpg'(长度=75)
公共“视频高度”=>int 720
公共“语言”=>字符串“en”(长度=2)
公共“视频比特率”=>float 309.2265625
公共'id'=>字符串'4010553696'(长度=10)
公开的“meta_游戏”=>字符串“星际争霸2:自由之翼”(长度=30)
公共“广播者”=>字符串“fme”(长度=3)
公共“广播部分”=>int 1
公共“音频编解码器”=>字符串“aac”(长度=3)
公共“最新时间”=>string“2012年10月19日星期五06:18:15”(长度=24)
公共“视频宽度”=>int1280
公共“geo”=>字符串“GR”(长度=2)
公共“频道视图计数”=>int 771541
公共“频道订阅”=>布尔值false
公共“已启用嵌入”=>布尔值true
公共“流类型”=>字符串“活动”(长度=4)
公共“视频编解码器”=>字符串“AVC”(长度=3)

您要做的是从两个集合(或者可能是列表,但我猜是集合)创建一个集合。要做到这一点,您需要确定唯一id是什么

在我的第一个示例中,unique_id只是所传递对象的键。在我的第二个示例中,它是一个字符串,通过连接来自该项的多个字段创建

第三个示例可能采用示例2中使用的唯一_id创建和示例1中使用的覆盖功能,但这将返回由唯一_id设置密钥的数组,因此可能不需要该数组

示例一:覆盖键控数组

function example_one_overwrite($old, $new){
    //THIS METHOD ASSUMES THAT KEY IS YOUR UNIQUE ID AND THAT YOU WANT A KEYED ARRAY RETURNED
    $unique = array();

    //DATA THAT MIGHT BE POSSIBLY OVERWRITTEN
    foreach($old as $key=>$item){
        $unique[$key] = $item;
    }

    //DATA THAT MIGHT POSSIBLY OVERWRITE OLD DATA
    foreach($new as $key=>$item){
        $unique[$key] = $item;
    }

    return $unique;
}
示例二:单次写入数组、数字键

function example_two_singlewrite($old, $new){

    //This method assumes you want a numeric array when you are done, not an associative one. 

    $unique = array();
    $used_unique_ids = array();

    //KEEP DATA
    foreach($new as $item){

        //I have this set to $item->name cause I don't know what you want to use to consider something unique
        //But another example might be $unique_id = $item->name.'-'.$item->format;

        $unique_id = $item->name;

        if(!isset($used_unique_ids[$unique_id])){
            $used_unique_ids[$unique_id] = "used";
            $unique[] = $item;
        }
    }

    //DATA TO KEEP ONLY IF IT WAS NOT IN THE OTHER LIST
    foreach($list as $item){

        $unique_id = $item->name;

        if(!isset($used_unique_ids[$unique_id])){
            $used_unique_ids[$unique_id] = "used";
            $unique[] = $item;
        }
    }

    return $unique;
}
如何在代码中使用这些示例

//NOTE: I'm not sure if you need the $key in your result, if so mod the example_two_singlewrite function.

//STARTING AFTER $old_datas_twitch = json_decode...
//REPLACE ALL CODE TILL $datas_push_twitch = file_put... WITH
$twitch_new_build = example_two_singlewrite($old_datas_twitch , $new_datas_twitch);

//ADD FUNCTION example_two_singlewrite to your file

您所说的
过滤新文件上的重复内容是什么意思?
过滤新文件上的重复内容?我需要取消设置多个旧值。有时,在$old_data数组中,数据不是一次重复,有时是两次、三次重复。所以,您希望所有新记录和旧记录中不在新记录中的记录?当我创建$twitch\u new\u build=array\u merge((array)时,能否从
foreach($old\u datas\u twitch as$key=>$value)
提供$value的var\u转储$