I'm trying to define a function that, given an associative array, would echo the key value pair, given an argument for that function.
So far I have the code that appears below. However, the result of that code would be a complete list (table) of the key-value pairs. What I'm trying to get is just ONE pair (once the function is called).
Could anybody help me?
Thanks!
enter code here
<!DOCTYPE html>
<html>
<head>
<body>
<h1>List of States</h1>
<?php
$states = array ("AL"=>"Alabama","AK"=>"Alaska","AZ"=>"Arizona","AR"=>"Arkansas","CA"=>"California","CO"=>"Colorado","CT"=>"Connecticut",
"DE"=>"Delaware","FL"=>"Florida","GA"=>"Georgia","HI"=>"Hawaii","ID"=>"Idaho","IL"=>"Illinois","IN"=>"Indiana","IA"=>"Iowa","KS"=>"Kansas",
"KY"=>"Kentucky");
function printState($Abbr) {
global $states;
echo "<table border=1>";
foreach($states as $Abbr => $value) {
echo "<tr>";
echo "<td>";
echo $Abbr;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
printState("CA");
?>
</body>
</html>
echo $states[$Abbr];suffice?echo $states["CA"];, you should get California (and only California).