当我添加新数组时,“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.

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 );        
                    }
                } */   
            }

The following is wrong:

$viewarray= utf8_encode(json_encode($viewarray,true));

You should not call utf8_encode on anything that is already utf-8 encoded. The output of json_encode is supposed to be utf-8, as it requires its argument to be utf-8.

In fact, calling utf8_encode can damage a valid string if it already has a multibyte encoding.

Besides that, make sure that your database is utf-8 (or similar) encoded, and your database connection is uft-8 encoded, ...etc.

When json_decode returns null be sure to check the string returned by json_last_error_msg().