简体   繁体   中英

PHP - How to store an arrays keys in an array and use it to later access the value

I want to save off an array of keys and use it like an index to get back to the corresponding value in the original array. Here's an example to illustrate.

$myArray = array("foo" => array("bar" => "Hello"));

the manual way to get to "Hello" would be

$helloString = $myArray["foo"]["bar"];

but I want a flexible way to do this for arrays with N number of keys something like

$keys = array("foo","bar");
$helloString = $myArray[$keys];  //doesn't work but hopefully shows my intent

Is there a way to do this without using eval()?

I guess you could do something like this:

<?php                                                                                                                                                                                                      
$myArray = array("foo" => array("bar" => "Hello"));                                                                                                                                                        
$keys = array("foo","bar");                                                                                                                                                                                

$item = $myArray;                                                                                                                                                                                          
foreach($keys as $k) {                                                                                                                                                                                     
    $item = $item[$k];                                                                                                                                                                                     
}                                                                                                                                                                                                                                                                                                                                                                                                                     
echo $item; //Hello                                                                                                                                                                                            
?>

Or stick it in a function:

function getValue($array, $keys) {                                                                                                                                                                                                                                                                                                                                                            
    foreach($keys as $k)                                                                                                                                                                               
        $array = $array[$k];                                                                                                                                                                                 

    return $array;                                                                                                                                                                                          
}

Don't really understand but try :

$keys = array("foo","bar");
$helloString = $myArray[$keys[0]][$keys[1]];

You can even try this one also

$myArray = array("foo" => array("bar" => "Hello"));                                                                                                                                                        
$keys = array("foo","bar");
function getval($myArray, $keys){
        $temp = $myArray[$keys[0]];
        if(!is_array($temp)){
            return $temp;
        }
        else
        {
            array_splice($keys,0,1);
            return getval($temp, $keys);
        }
}

//Here you go 
echo getval($myArray, $keys);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM