Viewed   55 times

I have the following json string and I want to retrieve just the email address from it. How do I do it in php?

{"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"}]}}

 Answers

2

Another twist on how inerte did it would be to access it like:

$json_object = '{"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"}
                         ]
                       }
                      }';


    $json_decoded = json_decode($json_object);
    echo "email: ".$json_decoded->communications->communication[3]->communicationValue."<br />";
Monday, November 28, 2022
5

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022
1

The reason to escape double quotes (") in a string, is if the string is double quoted.

Since you are escaping the double quotes, you should double (not single) quote your string, like this:

<?php
 $json = "{"json":[{"username":"1062576","accountId":"45656565"}]}";

 $postarray = json_decode($json);
 print_r($postarray);
?>

Live Example

If you do want to single quote your string, then don't escape the double quotes, or use stripslashes() like Andrei suggested.

You can read about the four ways to specify a string in PHP, and the differences among them, here.

Sunday, November 6, 2022
1

Given this JSON, you can get the currency of a country as follows:

function getCurrencyFor($arr, $findCountry) {
    foreach($arr as $country) {
        if ($country->name->common == $findCountry) {
            $currency = $country->currency[0];
            break;
        }
    }
    return $currency;
}

$json = file_get_contents("https://raw.githubusercontent.com/mledoze/countries/master/countries.json");
$arr = json_decode($json);
// Call our function to extract the currency for Angola:
$currency = getCurrencyFor($arr, "Angola");

echo "Angola has $currency as currency";
Sunday, October 30, 2022
 
4

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}
Thursday, October 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :