An associative array allows to store key/value pairs.
foreach
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>foreach</title>
</head>
<body>
<?php
$a = array ('one' => 1, 'two' => 2, 'three' => 3);
foreach ($a as $key_ => $value_) {
print "$key_ : $value_<br>";
}
?>
</body>
</html>
array_key_exists
array_key_exists($key, $ary)
returns TRUE
if the array $ary
contains the key $key
. Otherwise, it returns FALSE
.
Note that counterintuitively , IMHO, the first parameter is the key and the second the array!
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>array_key_exists</title>
</head>
<body>
<?php
$dict = array ('one' => 1, 'two' => 2, 'three' => 3);
if (array_key_exists('one', $dict)) echo "key <i>one</i> exists<br>"; else echo "key <i>one</i> does not exist<br>";
if (array_key_exists('foo', $dict)) echo "key <i>foo</i> exists<br>"; else echo "key <i>foo</i> does not exist<br>";
?>
</body>
</html>
See also the function
isset()
which checks if a
variable is declared and different from
NULL
.