php
  • json
  • 2010-05-25 13 views 5 likes 
    5

    Ho una variabile di sessione $_SESSION["animals"] contenente un oggetto JSON profondo con valori:JSON Cerca e rimuovi in ​​php?

    $_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"} 
    }'; 
    

    Per esempio, voglio trovare la riga con "Piranha Pesce" e quindi rimuoverlo (e json_encode di nuovo come era). Come fare questo? Credo di aver bisogno di cercare nell'array risultante json_decode($_SESSION["animals"],true) e trovare la chiave genitore da rimuovere ma sono comunque bloccato.

    risposta

    11

    json_decode trasforma l'oggetto JSON in una struttura PHP composta da matrici annidate. Quindi devi solo scorrere su di loro e unset quello che non vuoi.

    <?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); 
    ?> 
    
    +0

    Grazie! E come farlo se non conosco il nome della chiave? – moogeek

    +1

    Non è la soluzione migliore se non si conosce il nome della chiave. – Sarfraz

    +0

    @moogeek: intendi "tipo", "nome", "peso" e "età" in questo caso? Se non lo sai, dovrai introdurre un altro livello di iterazione, scorrendo attraverso '$ value' e controllando ogni sottovalore con la tua stringa. Se lo trovi, 'unset ($ animals [$ key])' funzionerà come sopra, e potrai uscire dal ciclo. Ho aggiunto questo codice alla mia risposta. –

    3

    Hai una virgola aggiuntiva alla fine dell'ultimo elemento nel tuo JSON. Rimuoverlo e json_decode restituirà un array. Passa semplicemente attraverso di esso, verifica la stringa, quindi disattiva l'elemento quando viene trovato.

    Se è necessario reindirizzare l'array finale, passare semplicemente a array_values.

    2

    questo funziona per 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); 
    
    ?> 
    
    Problemi correlati