6
votes

JSON Décode en PHP

J'ai la chaîne JSON suivante et je souhaite récupérer uniquement l'adresse e-mail. Comment puis-je le faire dans PHP? XXX


2 commentaires

Je souhaite que cette question a eu simplement: j'ai une chaîne codée JSON et souhaite récupérer l'adresse e-mail de celle-ci, mais ne comprenez pas comment utiliser json_decode et récupérer la valeur des résultats. Quelqu'un peut-il m'aider à faire cela dans PHP. Je suis habitué aux questions étant plus difficiles et celle-ci m'a jeté. :)


X-REF: Capable de voir une variable dans la sortie Print_R (), mais je ne sais pas comment y accéder dans le code


5 Réponses :


14
votes

Considérant que vous avez > JSON_Decode CODE> D Vos données de cette façon:

string 'email@needthis.com' (length=18)
string 'email@needthis.com' (length=18)


0 commentaires

6
votes

3 commentaires

Oui c'est ce que je veux mais je ne peux pas comprendre les détails? Toute aide est très appréciée.


Quel serait le nom de matrice et la clé pour l'adresse e-mail?


Imprimez la sortie en utilisant var_dump ou print_r et jetez un coup d'oeil là-bas



0
votes
<?php
$string = '{"communications":{"communication":[{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household":{"@id":"111111","@uri":"xyz/v1/Households/5465465"},"person":{"@id":"","@uri":""},"communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"},"communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/11111"},"person":{"@id":"2222222","@uri":"xyz/v1/People/22222222"},"communicationType":{"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"},"communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false","communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household":{"@id":"1111","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244404","@uri":"xyz/v1/People/1111"},"communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"},"communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244444474","@uri":"xyz/v1/People/111111"},"communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"},"communicationGeneralType":"Email","communicationValue":"email@needthis.com","searchCommunicationValue":"email@needthis.com","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"}]}}';

$encoded = json_decode($string, JSON_FORCE_OBJECT);

echo $encoded['communications']['communication'][3]['communicationValue'];

echo "\n";

echo $encoded['communications']['communication'][3]['searchCommunicationValue'];

?>
But if communicationValue or searchCommunicationValue ever get out of index 3 of communication, you're in trouble. You'll probably have to loop communication and search for these strings on its keys/values.

0 commentaires

8
votes

Une autre torsion sur la façon dont inerte serait-il d'y accéder comme: xxx


1 commentaires

C'est bien! C'est le travail parfaitement. Merci!



0
votes

Bel exemple écrit par moi

​​Passez la fonction JSON_OBJECT et 1 dans la fonction, vous verrez toutes les valeurs selon la structure. P>

function decodejson($value,$num){
    if (count($value,0) > 0 && is_array($value)){
        foreach ($value as $key =>$tvalue){
            if (is_array($tvalue)){
                //echo $key."-<br />";
                $num++;
                decodejson($tvalue,$num);
            }else
                echo str_repeat("&nbsp;", $num).$key."->".$tvalue."<br />";
        }
    }else
        echo str_repeat("&nbsp;", $num).$key."->".$value."<br />";
}


0 commentaires