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.