简体   繁体   English

PHP-如何在数组中存储数组键并在以后使用它访问值

[英]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 手动进入“ Hello”的方式是

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

but I want a flexible way to do this for arrays with N number of keys something like 但是我想要一种灵活的方法来对具有N个键的数组执行此操作,例如

$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()? 有没有不使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数组存储在文件中,以便以后使用PHP作为数组访问? - How do I store an array in a file to access as an array later with PHP? 如何在php中存储每个数组值及其键 - How to store each array value with their keys in php 如何在php中的会话变量中存储数组值并在以后使用它 - how to store array values in session variable in php and use it for later PHP:将数组输出存储在变量中,以备后用 - PHP: Store array output in a variable for later use 查询mysql表中具有相同月份值的行,并存储为php数组,以备后用在日历上显示它们 - query a mysql table for rows that have the same month value and store as a php array for later use displaying them on a calendar 在PHP的多维数组中为键数组使用变量 - Use A Variable for an Array of Keys in Multidimentional Arrays in PHP PHP将字符串限制为x,然后切成li并将其存储在一个值中以备后用 - PHP limit string to x then cut li and store it in a value for later use 如何在数据库中存储PHP条件文件以供以后使用? - How can I store a PHP conditional staement in the database for later use? 如何在 web 页面中存储 MySQL 值以供以后使用 - How to store a MySQL value in a web page for later use 使用 php 中的键/索引数组访问 php 中的深层数组值? - Access deep array value in php using an array of keys/indices in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM