0

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 expectancy) 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.

1

1 Answer 1

11
<?php
$country = $_POST['country']; // assuming post method and form element named 'country'
$ageExpectancy = $countries[$country];
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Didn't know I could actually call the same string with the variable itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.