0

I have a json array. in which i have 3 values. when i delete first or second value the resultant array after deletion is one format. But when i delete the last value always the resultant array changes. i am echoing the array by converting it back to string.

When i delete any value except the last one, that is like 1st or 2nd or any other value

result = {"1":{"name":"cat"},"2":{"name":"elephant"}}

Here i deleted first value - dog

$animals = '{
 "0":{"name":"dog"},
 "1":{"name":"cat"},
 "2":{"name":"elephant"}
 }';
$animals = json_decode($animals, true);
$del_value = "elephant";
$loginArray = array('name' => $del_value);
if (in_array($loginArray, $animals)) {
    foreach ($animals as $key => $value) {
        if (in_array($del_value, $value)) {
            unset($animals[$key]);
        }
    }
}
$animals_string = json_encode($animals);
echo $animals_string;

but When i delete the last value it creates different format

result = [{"name":"dog"},{"name":"cat"}]

Here i deleted last value - elephant

$animals = '{
 "0":{"name":"dog"},
 "1":{"name":"cat"},
 "2":{"name":"elephant"}
 }';
$animals = json_decode($animals, true);
$del_value = "elephant";
$loginArray = array('name' => $del_value);
if (in_array($loginArray, $animals)) {
    foreach ($animals as $key => $value) {
        if (in_array($del_value, $value)) {
            unset($animals[$key]);
        }
    }
}
$animals_string = json_encode($animals);
echo $animals_string;
2
  • When you say different format do you mean it becomes an array instead of an object with numeric indexes? If that's the case then that's normal. Commented Sep 8, 2016 at 8:07
  • just check please,, its not normal.. when i delete first or second value it produces array in one format,, but if i delete last value it produces another format of array. Commented Sep 8, 2016 at 8:11

1 Answer 1

1

This behaviour is expected.

PHP supports 2 type of array:

  1. Associative: where indexes can be strings or non-sequential integers
  2. Numerically indexed: Where indexes are sequential integers starting at 0

However JSON only supports the 2nd type of array, therefore when you convert an associative array to JSON you get an object instead.

Example:

print_r(json_encode([ 1,2,3,4 ])); //Prints [1,2,3,4]
print_r(json_encode([ 0=> 1, 1=>2, 2=>3, 3=>4 ])); //also prints [1,2,3,4]
print_r(json_encode([ "a" => 1, "b" => 2 ])); //Prints {"a":1,"b":2}
print_r(json_encode([ 1 => "a" , 2 => "b" ])); //Prints {"1":"a","2":"b"}

Force PHP array (of any type) to JSON array by dropping original keys:

$animals_string = json_encode(array_values($animals)); //Will always have form : [{"name": "X" },{"name":"Y"}]

Force PHP array (of any type) to JSON object:

$animals_string = json_encode((object)$animals); //Will always have form : {"i1":{"name":"X"},"i2":{"name":"Y"}}

The problem in your particular case is that your JSON object gets translated into a PHP numerically indexed array which when encoded back it becomes a JSON array:

$animals = '{
   "0":{"name":"dog"},
   "1":{"name":"cat"},
   "2":{"name":"elephant"}
 }';
$animals = json_decode($animals, true);

echo json_encode($animals); 

Echoes

[{"name":"dog"},{"name":"cat"},{"name":"elephant"}]

If you remove one of the indexes other than the last then this makes the PHP array associative which gets translated to a JSON object.

This is is how PHP works if you want a different output you need to be explicit about it like below:

$animals = '{
 "0":{"name":"dog"},
 "1":{"name":"cat"},
 "2":{"name":"elephant"}
 }';
$animals = json_decode($animals, true);
$del_value = "elephant";
$loginArray = array('name' => $del_value);
if (in_array($loginArray, $animals)) {
    foreach ($animals as $key => $value) {
        if (in_array($del_value, $value)) {
            unset($animals[$key]);
        }
    }
}
$animals_string = json_encode((object)$animals);
echo $animals_string;  //Always the same format

I hope this helps.

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

7 Comments

you didnt understand.. i dont have any problem with what you have said. i appreaciate your help. thank you. but my problem is ,, what is the matter with deleting 1st/ 2nd value vs deleting last value ,, suppose there are 3 values ,, i delete only 1st value ,, after that i have result array. ok fine. if i delete 2nd value from those 3 values . i got a result array. in both cases the result array is in same format. but if i delete the last value the result array is in another format. it is not like that i am deleting the 1st, 2nd, 3rd value respectively. all are different cases.
i am saying there is difference -- "(case 1) deleting 1st or 2nd or 3rd or nth value" compare to "(case 2) deleting last value"
@SurajRoy I've added some more information. The point is when you do json_decode($animals, true) you're converting a JSON object to a PHP numerically indexed array. When you unset something in the start or middle of the array this makes the array associative, while unsetting something at the end keeps it numerically indexed.
So,, this is the problem.. now i understand. so,, if i check before deleting if the value is last value or not ,, then i can add some condition to the delete process. so,, how can i check if the value is last value or not ?
if ($key == array_keys($animals)[count($animals)-1]) { /* its the last key */ }
|

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.