1

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>
5
  • 3
    Wouldn't echo $states[$Abbr]; suffice? Commented Jun 28, 2016 at 16:55
  • So you just want to print 1 key/value pair each time you call the function OR do you want to get the specific value to that key which you pass to the function? Commented Jun 28, 2016 at 16:56
  • Hey. I just want to print ONE pair, once the function is called. Meaning, if I passed the key "CA", I'd like to get a table just with one row, that includes the key (CA) and the state name (California). BTW, I tried echoing $states[$Abbr];, but I get the complete list of states. Commented Jun 28, 2016 at 17:27
  • @EmilioZaidman That shouldn't happen based on your code. If you remove all of the code, except for the array, and do something like echo $states["CA"];, you should get California (and only California). Commented Jun 28, 2016 at 17:51
  • That's correct. I did that and worked. But I'm only getting the value, not the key. The code that Jamie gave me (I don't know how to mention him) worked just fine. Thank you! Commented Jun 28, 2016 at 18:32

2 Answers 2

4

If you must have a function:

function getState($code) {
    global $states;
    if (array_key_exists($code, $states)) {
        return $states[$code];
    }
    return false;
}

echo getState('GA');

But as Dave Chen suggested, $states[$abbr]; is how you'd do it.

Sign up to request clarification or add additional context in comments.

1 Comment

But in_array checks if the value exists as a value and $code would be the key to the array. I think it should be array_key_exists
0

Hi You can also use this

function printState($Abbr) {
    global $states;
if (array_key_exists($Abbr,$states)){   
    return "<table border=1><tr><td>$Abbr</td><td>$states[$Abbr]</td></tr></table>";
    }
}
echo printState("AK");
?>

Comments

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.