I have a person array, with a key ‘n’ for name. The value for ‘n’ is an array, with key ‘l’ (short for ‘last’) having the value I want to get at, the last name.
For curious programming reasons, I have “address” of the last name as a string.
How do I do this?
I’ve tried eval, among others.
SORRY I wasn’t being clear!!
What I want is to use the variable $test to get it.
E.g. something like:
$person[$test]
(which obviously doesn’t work, but what does?)
$test won’t always be ‘two deep’. E.g. sometimes it will be ['a']['residential']['street']
<?php
$test = "['n']['l']";
echo "I've got a string with the array index ".$test. "\n";
$person = array("n"=>array('l'=>'SMITH'));
echo 'the array, $person, is this: '."\n";
print_r($person);
echo "What I want is ".$person['n']['l']. "\n";
?>
Output:
I've got a string with the array index ['n']['l']
the array, $person, is this:
Array
(
[n] => Array
(
[l] => SMITH
)
)
What I want is SMITH
You pretty much have only two options:
Using eval:
Or string parsing:
Both output: