PHP delete an array inside an array -


i trying open .json file , check if file contains array identified key. if so, want delete , write file server. however, not having luck , think 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 $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 page",         "a"     ],     "bdrehgzgxf": [         "wild nights",         "a"     ],     "oencwqzjs": [         "every day winding road",         "b,j"     ],     "j8suv-ykob": [         "testin",         "b"     ] } 

in case can avoid loop because of way data structured, can directly call specific array of data in $song_list array.

your song id keys can go directly , check if key exists in song_list array, if exist unset key.

here in action:

<?php $song_id = "j8suv-ykob"; $file = "file.json"; $song_list = json_decode(file_get_contents($file),true);  if (array_key_exists($song_id, $song_list)) {     unset($song_list[$song_id]);         file_put_contents($file,json_encode($song_list,json_numeric_check | json_pretty_print)); } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -