开发者

PHP: Linking an array value with a variable

I have an array that stores names of countries (keys) and age expectancy (values).

$countries = array(  
  'Costa Rica' => 78, 'Puerto Rico' => 78, 'Colombia' => 73, 
  'Nicaragua' => 73, 'El Salvador' => 72, 'Peru' => 71,
  'Argentina' => 75, 'Uruguay' => 76, 'Ecuador' => 75,
  'Mexico' => 76, 'Venezuela' => 73, 'Brasil' => 72,
);

An html form creates a new variable ($country) with a name that corresponds to one countries listed above. For example: "Costa Rica"

How can I return a variable with the number (age expectan开发者_如何学编程cy) that matches the name of the country in the $country variable?

Take for example:

Costa Rica = 78
$ageExpectancy = 78;

Thanks, I hope to be concise.


<?php
$country = $_POST['country']; // assuming post method and form element named 'country'
$ageExpectancy = $countries[$country];
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜