2016-08-16 8 views
0
<?php function getCurrencyFor($arr, $findCountry) { 
    foreach($arr as $country) { 
     if ($country->name->common == $findCountry) { 
      $currency = $country->currency[0]; 
      $capital = $country->capital; 
      $region = $country->region; 

      break; 
     } 
    } 
    return $country(); 
} 

$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, "Aruba"); 

      echo $country('$capital'); 
      echo $country('$currency'); 
      echo $country('$region'); 



?> 

関連するオブジェクトを取得し、私はこの記事に続く - https://stackoverflow.com/a/38906191/3939981.JSONファイル - 値を検索し、

私は関数内のコードを書き換えた場合、それはたぶん、いくつかの部分

<?php function getCurrencyFor($arr, $findCountry) { 
    foreach($arr as $country) { 
     if ($country->name->common == $findCountry) { 
      $currency = $country->currency[0]; 
      $capital = $country->capital; 
      $region = $country->region; 
      echo $capital; 
      echo $currency; 
      echo $region; 
      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, "Aruba"); 
?> 

の作品すべてのコメントと考えて

答えて

0

このコードを使用することができます。関数が3つの値を返すようにするには、それらの値を持つ配列を作成し、その配列を返す必要があることに注意してください。

function getCountryInfo($arr, $findCountry) { 
    foreach($arr as $country) { 
     if ($country->name->common == $findCountry) { 
      return array(
       "currency" => $country->currency[0], 
       "capital" => $country->capital, 
       "region" => $country->region 
      ); 
     } 
    } 
} 

$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: 
$country = getCountryInfo($arr, "Aruba"); 

echo $country['capital'] . "<br>"; 
echo $country['currency'] . "<br>"; 
echo $country['region'] . "<br>"; 
+0

私はもう一度手伝ってくれてうれしいです...これはうまくいきます... 100万ドルのおかげで、私は関数の名前を変更しました。 –

+0

回答を受け入れたものとしてマークすることを忘れないでください。 – trincot

+0

その素晴らしい...私は –

関連する問題