Sheju Sathyanesan Sheju Sathyanesan - 2 months ago 11x
JSON Question


I am doing some JSON decodes - I followed this tutorial well explained - How To Parse JSON With PHP

and the PHP code, I used




// array method
foreach($json_a[person] as $p)
echo '

Name: '.$p[name][first].' '.$p[name][last].'

Age: '.$p[age].'



// object method
foreach($json_o->person as $p)
echo '

<br/> Name: '.$p->name->first.' '.$p->name->last.'

Age: '.$p->age.'



It is working correctly... But my concern I need only details of Thomas' last name and age. I need to handle this to extract only certain features, not all the objects.


Give the JSON data that you provided the link to, this should return the currency value for the given country:

$country_data = json_decode(file_get_contents(""), TRUE);

function get_currency($name) {
    global $country_data;

    $name = strtolower($name);
    $output = reset(array_filter($country_data, function ($value, $key) use($name) {
        if(strtolower($value['name']['common']) === $name || strtolower($value['name']['official']) === $name) {
            return true;
    }, ARRAY_FILTER_USE_BOTH))['currency'];
    return ($output) ? $output : array();

/* Return same results */

echo "<pre>";
print_r(get_currency("Islamic Republic of Afghanistan"));
echo "</pre>";

echo "<pre>";
echo "</pre>";

NOTE: The above function is case-insensitive. If you need case sensitivity supported, remove the strtolower() function references.


  • Corrected a bug in the snippet.


  • Returns an array of currencies if the country name is found or an empty array array() if the country cannot be found.
  • The name passed into get_currency() is now checked against the common name and the official name. Passing either will return a value.