views:

88

answers:

3

Hello!

I have a session variable $_SESSION["animals"] containing a deep json object with values:

$_SESSION["animals"]='{
"0":{"kind":"mammal","name":"Pussy the Cat","weight":"12kg","age":"5"},
"1":{"kind":"mammal","name":"Roxy the Dog","weight":"25kg","age":"8"},
"2":{"kind":"fish","name":"Piranha the Fish","weight":"1kg","age":"1"},
"3":{"kind":"bird","name":"Einstein the Parrot","weight":"0.5kg","age":"4"}
}'; 

For example, I want to find the line with "Piranha the Fish" and then remove it (and json_encode it again as it was). How to do this? I guess i need to search in json_decode($_SESSION["animals"],true) resulting array and find the parent key to remove but i'm stucked anyways.

+3  A: 

json_decode will turn the JSON object into a PHP structure made up of nested arrays. Then you just need to loop through them and unset the one you don't want.

<?php
$animals = '{
 "0":{"kind":"mammal","name":"Pussy the Cat","weight":"12kg","age":"5"},
 "1":{"kind":"mammal","name":"Roxy the Dog","weight":"25kg","age":"8"},
 "2":{"kind":"fish","name":"Piranha the Fish","weight":"1kg","age":"1"},
 "3":{"kind":"bird","name":"Einstein the Parrot","weight":"0.5kg","age":"4"}
 }';

$animals = json_decode($animals, true);
foreach ($animals as $key => $value) {
    if (in_array('Piranha the Fish', $value)) {
        unset($animals[$key]);
    }
}
$animals = json_encode($animals);
?>
Samir Talwar
Thanks! And how to do this if I don't know the key name?
moogeek
It is not the better solution if one does not know the key name.
Sarfraz
@moogeek: Do you mean "kind", "name", "weight" and "age" in this case? If you don't know that, you'll need to introduce another layer of iteration, looping through `$value` and checking each sub-value against your string. If you find it, `unset($animals[$key])` will work as it does above, and you can then break out of the loop. I've added this code to my answer.
Samir Talwar
I've just realised it's much easier with `in_array`. See my answer for details.
Samir Talwar
+2  A: 

You have an extra comma at the end of the last element in your JSON. Remove it and json_decode will return an array. Simply loop through it, test for string, then unset the element when found.

If you need the final array reindexed, simply pass it to array_values.

webbiedave
+1  A: 

This works for me:

#!/usr/bin/env php 
<?php

    function remove_json_row($json, $field, $to_find) {

        for($i = 0, $len = count($json); $i < $len; ++$i) {
            if ($json[$i][$field] === $to_find) {
                array_splice($json, $i, 1); 
            }   
        }   

        return $json;
    }   

    $animals =
'{
"0":{"kind":"mammal","name":"Pussy the Cat","weight":"12kg","age":"5"},
"1":{"kind":"mammal","name":"Roxy the Dog","weight":"25kg","age":"8"},
"2":{"kind":"fish","name":"Piranha the Fish","weight":"1kg","age":"1"},
"3":{"kind":"bird","name":"Einstein the Parrot","weight":"0.5kg","age":"4"}
}';

    $decoded = json_decode($animals, true);

    print_r($decoded);

    $decoded = remove_json_row($decoded, 'name', 'Piranha the Fish');

    print_r($decoded);

?>
Dan Beam