doujia2090 2018-01-29 19:35
浏览 64
已采纳

当我添加新数组时,“A”会不断添加到多维数组的开头。 是什么导致这个?

I have an odd issue. I have a multidimensional array in an SQL database. I add additional arrays into the array on a weekly basis via cron job. It works well for the most part but sometimes a random A gets added to the array when a new array is added. Does anyone have any idea as to what can cause this? It makes the json_decode result null when fetched.

Here is what the clean array looks like in the database after a new item has been added:

{"UCUVa51UA_690sEKyRbHb-5A":{"1":"1816468"},"UCfagwFCjnHBYRYIyBnmNAdA":{"1":"39839"}}

Here is what randomly happens sometimes a new item is added:

A"UCUVa51UA_690sEKyRbHb-5A":{"1":"64,596"},"UCfagwFCjnHBYRYIyBnmNAdA":{"1":"16,756"},"UCk2KE7yg0BwsJfr8Dp9ivUQ":{"1":"175,859"}}

It's a bit frustrating. Here is the snippet of script that adds the new array to the existing one:

foreach( $request['author_channel'] as $key => $value ){
        if ($value['platform'] == 'Youtube' || $value['platform'] == 'youtube'){
            $channelidtwo=$value['channel'];

            //converting channel id to url
            $mainchannelid=$value['channel'];
            $link=$request['author_channel'][$key]['channellink'];
            //this is where we're messing up
            $request['author_channel'][$key]['channellink']="https://www.youtube.com/channel/".$channelidtwo;
            $channelid=array('id' => $value['channel']);
            // $channelid=array('id' => 'UCdHUJoh8Si5V88m4ObkS7FA');
            //running hardy's function on the id of the channel
            $subscribers=subscribersById($channelid);
            //running hardy's function on the id of the channel
            $title=titleById($channelid);

            $avgviews=addavgviews($channelidtwo);

            $avgviews=number_format($avgviews);

            $request['author_channel'][$key]['channelname'] = $title;


            $request['author_channel'][$key]['subscribers'] = number_format($subscribers);
            if( !empty($subscribers) ){
                $request['author_channel'][$key]['subscriber'] = number_format($subscribers);
            }
            if( !empty($title) ){
                $request['author_channel'][$key]['channelname'] = $title;
            // update_post_meta( $result->ID, 'channel_video', $request['author_channel'] );
            }

            $currentyoutube=get_post_meta($result->ID, 'youtube_subscribers', true );
            if (empty($currentyoutube)){
                $channelid=$value['channel'];
                $newyoutubearray=array($channelid=>array('1'=>$subscribers));
                $newyoutubearray= utf8_encode(json_encode($newyoutubearray,true));
                update_post_meta( $result->ID, 'youtube_subscribers', $newyoutubearray );
            }else{
                $viewarray=json_decode($currentyoutube,true);
                $channelid=$mainchannelid;
                $addyoutubearray=array('1'=>$subscribers);
                foreach ($viewarray as $key=> $value){
                    if ($key !==$channelid){
                        $viewarray[$channelid]=$addyoutubearray;
                        $viewarray= utf8_encode(json_encode($viewarray,true));
                        update_post_meta( $result->ID, 'youtube_subscribers', $viewarray );        
                    }
                }    
            }

I apologize for my poor style, I'm newer to web development. If there is any other information you need please let me know. Thank you for the help!

**update- so this "A" only happens when the third item is added to the array. Not sure what could be causing this.

  • 写回答

2条回答 默认 最新

  • drvjlec1767 2018-02-03 18:00
    关注

    I was able to figure it out. Providing the answer here in case anyone ever encounters the same problem.

    I really appreciate the answers to help solve the issue as well.

    Here is the updated code that worked:

        foreach( $request['author_channel'] as $key => $value ){
            $platform=$value['platform'];
            if ($platform == 'Youtube' || $platform == 'youtube'){
                $channelidtwo=$value['channel'];
    
                //converting channel id to url
                $mainchannelid=$value['channel'];
                $link=$request['author_channel'][$key]['channellink'];
                $request['author_channel'][$key]['channelid']= $channelidtwo;
                $request['author_channel'][$key]['channellink']="https://www.youtube.com/channel/".$channelidtwo;
                $channelid=array('id' => $value['channel']);
    
                //running youtube functions on the id of the channel
                $subscribers=subscribersById($channelid);
                $totalviews=totalviews($channelid);
                $totalvideos=getvideocount($channelid);
                $title=titleById($channelid);                
                $avgviews=addavgviews($channelidtwo);
    
                $request['author_channel'][$key]['channelname'] = $title;
    
                $request['author_channel'][$key]['subscribers'] = ($subscribers);
                if( !empty($subscribers) ){
                    $request['author_channel'][$key]['subscriber'] = ($subscribers);
                }
                if( !empty($title) ){
                    $request['author_channel'][$key]['channelname'] = $title;
                 update_post_meta( $result->ID, 'channel_video', $request['author_channel'] );
                }
    
                $currentyoutube=get_post_meta($result->ID, 'youtube_subscribers', true );
                if (empty($currentyoutube)){
                    $channelid=$mainchannelid;
                    $newyoutubearray=array($channelid=>array('1'=>$subscribers));
                    $newyoutubearray= json_encode($newyoutubearray);
                    update_post_meta( $result->ID, 'youtube_subscribers', $newyoutubearray );
                }else{
                    $viewarray=json_decode($currentyoutube,true);
    
                    //here's what I added to obtain array keys and insert multidimensional array without alter key syntax
                    //I commented out the incorrect foreach key test below to show the difference
                    $viewChannelIds = array_keys($viewarray);
    
                    $channelid=$mainchannelid;
                    $addyoutubearray=array('1'=>$subscribers);
                    if( !in_array( $channelid, $viewChannelIds ) ){
                        $viewarray[$channelid]=$addyoutubearray;
                        $viewarray=json_encode($viewarray);
                        update_post_meta( $result->ID, 'youtube_subscribers', $viewarray );        
                    }
                    /*foreach ($viewarray as $key => $value){
                        if ($key != $channelid){
                            $viewarray[$channelid]=$addyoutubearray;
                            if (mb_substr($viewarray,0,2)=='"a'){
                                $viewarray=substr($viewarray,2);
                            }
                            $viewarray=json_encode($viewarray);
                            echo $viewarray;
                            //update_post_meta( $result->ID, 'youtube_subscribers', $viewarray );        
                        }
                    } */   
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作