-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassoc_arrays.php
executable file
·103 lines (75 loc) · 1.96 KB
/
assoc_arrays.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<form action="assoc_arrays.php" method="post">
<label for="">Enter a country</label>
<input type="text" name="country">
<input type="submit" value="enter">
</form>
</body>
</html>
<?php
// associative array = An array made of key=>value pairs
// countries => capitals
// id => username
// item => price
$capitals = array(
"USA"=>"Washington D.C.",
"Japan"=>"Kyoto",
"South Korea"=>"Seoul",
"India"=>"New Delhi"
);
// echo "The capitol of the U.S.A is: " . $capitals["USA"];
// $capitals["USA"] = "Las Vegas";
$capitals["China"] = "Beijing";
// array_pop($capitals);
// array_shift($capitals);
// $keys = array_keys($capitals);
// $values = array_values($capitals);
// $capitals = array_flip($capitals);
// $capitals = array_reverse($capitals);
// foreach ($capitals as $key => $value)
// {
// echo $key . " = " . $value . "<br>";
// }
// echo "<br>";
// foreach ($keys as $key)
// {
// echo $key . "<br>";
// }
// echo "<br>";
// foreach ($values as $value)
// {
// echo "{$value} <br>";
// }
// echo count($capitals);
// ----------------------------------
// $country = $_POST["country"];
// echo "<br><br>" . "The capitol of " . $country . " is " . $capitals[$country];
$capital = $capitals[$_POST["country"]];
// if(empty($capital))
// {
// echo "<br><br>That country is not in our database.";
// }
// else
// {
// echo "<br><br>The capitol of " . $_POST["country"] . " is " . $capital;
// }
if(isset($capital))
{
echo "<br><br>The capitol of " . $_POST["country"] . " is " . $capital;
}
else
{
echo "<br><br>That country is not in our database.<br><br>";
foreach ($capitals as $key => $value)
{
echo $key . " = " . $value . "<br>";
}
}
?>