I am trying to open a .json file and check if the file contains an array identified by its key. If so, I want to delete it and write the file back to the server. However, I am not having any luck and I think it is my misunderstanding of arrays of arrays in PHP:
PHP:
$song_id = "j8sUV-ykOB";
$file = "file.json";
$song_list = json_decode(file_get_contents($file),true);
foreach($song_list as $song){
if(in_array($song_id,$song_list)){
unset($song);
file_put_contents($file,json_encode($song_list,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT));
}
}
file.json:
{
"uKrb3eNCf": [
"Thunder Rolls",
"c"
],
"kdOzCKjKN-": [
"Turn the Page",
"a"
],
"bDreHgZgxF": [
"Wild Nights",
"a"
],
"oeNcwqZJS": [
"Every day is a winding road",
"b,j"
],
"j8sUV-ykOB": [
"Testin",
"b"
]
}