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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化