0

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"
    ]
}
3
  • I'm voting to close this question as off-topic because you should first try to do your tasks by yourself in first place. Commented Aug 18, 2017 at 16:47
  • What is your error ?.. Commented Aug 18, 2017 at 16:47
  • @MarcinOrlowski The code provided is the attempt by OP. Commented Aug 18, 2017 at 17:00

3 Answers 3

2

In your case you can avoid a for loop because of the way your data is structured, we can directly call to the specific array of data in your $song_list array.

Your song ID are keys so you can go directly and check if the key exists in your song_list array, if it does exist then unset that key.

Here is it 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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Should the file_put_contents() go inside the if statement?
@TripleC you are quite correct, it should, I will edit to reflect that.
1

What you want is to unset the $song_list[$key], not the $song variable:

foreach($song_list as $key => $song){
  if(in_array($song_id,$song_list)){
    unset($song_list[$key]);          
  } 
}
file_put_contents($file,json_encode($song_list,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT));

The $song variable in your code contains the value of the current element in your json file, doing unset on that value makes no sense.

4 Comments

it would be helpful to OP to provide an explanation of why unsetting $song doesn't work rather than just providing the right answer
@Aknosis multiples edits and missed that one :) Thanks for noticing that. Added now.
Reading more carefully, there isn't any reason for the in_array check inside the loop. This could be done outside the loop, but doesn't actually need to be executed at all (with the iterating code)
Using a foreach construct in this question is un-needed as we can reference the key directly using the song_id. Using a array_key_exists check is more efficient to check. Your code here is doing a complete check of the array in the foreach and then inside that it is doing the same thing with the in_array function so this check is O(n)^2 in comparison to an array_key_exists check which is O(n) but almost O(1) as it does not iterate through every single object.
0
<?php
$song_id = "j8sUV-ykOB";
$file = "file.json";
$song_list = json_decode(file_get_contents($file),true);
$new_array = array();

foreach($song_list as $key => $song){
    if($key != $song_id){
        $new_array[$key] = $song; 
    }  
}

file_put_contents($file,json_encode($new_array,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT));
?>

This code is successfully work foe me. In foreach check $song_id is equal to Key of array or not. if not insert this array in new array. So after complete all loop of foreach, new array created. So write json file again after decoded this new array. Thanks

1 Comment

There is no need to go through each index if each array has a unique key which we know and can call directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.