开发者

PHP: Linking an array value with a variable

开发者 https://www.devze.com 2023-02-24 21:30 出处:网络
I have an array that stores names of countries (keys) and age expectancy (values). $countries = array(

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

精彩评论

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

关注公众号