1

I am facing a strange issue with my PHP code (I'm a beginner to PHP so apologies for my bad coding skills). Each array item in my JSON has a unique ID associated with it, to delete an array I just pass the unique ID to my code and it deletes the array item associated with it, but all my array items contain an integer field and that doesn't get deleted and it messes up my JSON (parsing fails when I try to do so later).

<?php
$var1 = filter_input(INPUT_POST, 'unique_id', FILTER_UNSAFE_RAW);
if ($var1 === null) {
    die('The "unique_id" parameter is not set');
}
$data = file_get_contents('feed.json');
if ($data === false) {
    die('An error occurred when opening "feed.json"');
}
$json = json_decode($data, true);
if ( ! isset($json[0]['unique_id'])) {
    die("The JSON was not decoded correctly");
}
foreach ($json as $key => $value) {
    if ($value['unique_id'] == $var1) {
        unset($json[$key]);
    }
}
$new_json_string = json_encode($json);
file_put_contents('feed.json', $new_json_string, JSON_UNESCAPED_SLASHES | LOCK_EX);
echo "Success";
?>

Here's the sample JSON:

[
    {"student_id":"22222222","unique_id":"862916786a1340afbfdf23caa541963f","status":"Hey yo what's up","image":"none","likes":"0"},
    {"student_id":"33333333","unique_id":"d237556a90d44b1397e9290cd8g09349","status":"Message from another student","image":"none","likes":22}
]

after deleting, I'm left with

{"1":{"student_id":"33333333","unique_id":"d237556a90d44b1397e9290cd8g09349","status":"Message from another student","image":"","likes":31}

As you can see {"1": is invalid and shouldn't be there.

Does anyone know what am I doing wrong?

EDIT: Here's my code to create a new array item in my JSON

$json = file_get_contents('feed.json');
$data = json_decode($json);
$data[] = array('student_id' => "$student_id", 'unique_id' => "$unique_id" ,'status' => "$status_txt", 'image' => "$image_link", 'likes' => "0");
file_put_contents('feed.json', json_encode($data), JSON_UNESCAPED_SLASHES | LOCK_EX);
1
  • 1
    Just reset indexes to the normal order by $new_json_string = json_encode(array_values($json)); Commented Nov 21, 2014 at 23:40

2 Answers 2

1

This is a combination of two things:

There is no representation of sparse Arrays in JSON

JSON's arrays with [] can only have a comma-separated list of contiguous elements starting from index 0.

No type information distinguishes JSON arrays and JSON objects in PHP

Both of these things are stored as PHP's array type. JSON objects use the associative keys.


When you unset index 0 from your array, it becomes sparse. Now, the only way to encode something that has a something[1] but no something[0] in JSON is to have an object with a key "1".

PHP's JSON encoder allows this because the array you pass in is of the right type (array) to be serialized as a JSON object. So it does this.

Maybe you wanted to use array_splice to remove the array element instead of unset.

Sign up to request clarification or add additional context in comments.

3 Comments

Is that comment specific to this answer?
No, I just wanted to add that creating array elements and modifying them works flawlessly, but deleting doesn't, and I didn't fully understand your answer.
@SomeProgrammer123 He just tried to explain that when elements' indexes go as 0, 1, 2, 3, ... and you remove, let say, element with index 2, I've got 0, 1, 3, .... php creates a 1-to-1 json, so that it will be recreated exactly to the same array, so it should list indexes too. If you reset the indexes to the normal flow, as I showed you, there is no need to keep those indexes in json, so they are not there.
0

Use array_values() to convert array as notified by Cheery in comments.

Comments

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.