PHP associative arrays explained

preview_player
Показать описание
#PHP #course #tutorial

00:00:00 intro
00:00:28 associative array
00:02:27 loop through an associative array
00:03:25 array functions
00:08:05 exercise
00:10:58 conclusion
Рекомендации по теме
Комментарии
Автор

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="index.php" method="post">
<label>Enter a country: </label>
<input type="text" name="country">
<input type="submit">
</form>
</body>
</html>
<?php
// associative array = An array made of key=>value pairs

$capitals = array("USA"=>"Washington D.C.",
"Japan"=>"Kyoto",
"Australia"=>"Canberra",
"India"=>"New Delhi");

$capital = $capitals[$_POST["country"]];
echo"The capital is {$capital}";
?>

BroCodez
Автор

I used a foreach loop as an alternative, but I see your approach is better

kristijanlazarev
Автор

Thank you bro! Just a thought, isn't Japan's capital Tokyo?

lucasfernandes