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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?