0

I have an array of values

array(
    'FDR' => 'Franklin D Roosevelt',
    'JFK' => 'John F Kennedy'
)

and I have a variable

$variable = 'FDR'

I want to change the variable to say Franklin D Roosevelt based on the fact that the array points FDR to Franklin D Roosevelt.

1 Answer 1

1

Here's the simplest solution based on your description:

$arr = array('FDR' => 'Franklin D Roosevelt', 'JFK' => 'John F Kennedy');

$var = 'FDR';

$var = (array_key_exists($var, $arr) ? $arr[$var] : $var);

echo $var; // Franklin D Roosevelt

Or another way:

$arr = array('FDR' => 'Franklin D Roosevelt', 'JFK' => 'John F Kennedy');

$var = 'FDR';

foreach($arr as $i=>$a) {
    if ($i == $var) {
        $var = $a;
        break;
    }
}

echo $var; // Franklin D Roosevelt
Sign up to request clarification or add additional context in comments.

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.