JSON DECODE using PHP

折月煮酒 提交于 2019-12-10 12:27:29

问题


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

and the PHP code, I used

 <?php
 $string='{"person":[
            {
                "name":{"first":"John","last":"Adams"},
                "age":"40"
            },
            {
                "name":{"first":"Thomas","last":"Jefferson"},
                "age":"35"
            }
         ]}';

$json_a=json_decode($string,true);

$json_o=json_decode($string);


// 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.


回答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";



回答2:


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("https://raw.githubusercontent.com/mledoze/countries/master/countries.json"), 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>";
print_r(get_currency("Afghanistan"));
echo "</pre>";

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

EDIT:

  • Corrected a bug in the snippet.

EDIT 2:

  • 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.



回答3:


Actually Thomas is the first name not last name try this code..

print "names:", ",".join(x["last"] for x in obj if x["first"] == "Thomas")



来源:https://stackoverflow.com/questions/38905683/json-decode-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!