PHP:我们能在多维数组中动态地获取想要的任何键吗?

PHP:我们能在多维数组中动态地获取想要的任何键吗?,php,arrays,multidimensional-array,dynamically-generated,Php,Arrays,Multidimensional Array,Dynamically Generated,我们能在多维数组中动态地获得想要的任何键吗 $f['Kitchen']['Dishes']['Mantovarka']=3; $f['Kitchen']['Dishes']['Castrool']=91; $f['Kitchen']['Dishes']['Separator']=10; $f['Kitchen']['Product']=18; $f['Kitchen']['Textile']=19; $f['Kitchen']['Blue things']['Juicemaker']=25; $

我们能在多维数组中动态地获得想要的任何键吗

$f['Kitchen']['Dishes']['Mantovarka']=3;
$f['Kitchen']['Dishes']['Castrool']=91;
$f['Kitchen']['Dishes']['Separator']=10;
$f['Kitchen']['Product']=18;
$f['Kitchen']['Textile']=19;
$f['Kitchen']['Blue things']['Juicemaker']=25;
$f['Kitchen']['Blue things']['Freegener']=13;
$f['Kitchen1']['Blue things One']['Microwave']=4;
$f['Kitchen1']['Blue things One']['Iron']=24;
$f['Kitchen1']['Dishes One']['Separator']=110;
例如,如果我想分别到达“厨房1”和“盘子”以及“熨斗”

 var_dump($f['Kitchen1']);
 Output:
 [
    'Blue things One' => [
        'Microwave' => 4
        'Iron' => 24
    ]
    'Dishes One' => [
    'Separator' => 110
    ]
 ]

 var_dump($f['Kitchen']['Dishes']);
 Output:
 [
    'Mantovarka' => 3
    'Castrool' => 91
    'Separator' => 10
 ]

 var_dump($f['Kitchen1']['Blue things']['Iron']);
 Output:
 24
我试图通过以下方式达到目的:

 $kitchenKeys = "{'Kithcen1'}"; 
 var_dump($f[$kitchenKeys]);
 Output:
 NULL

 $dishKeys = "{'Kitchen'}][{'Dishes'}";
 var_dump($f[$dishKeys]);
 Output:
 NULL

 $ironKeys = "{'Kitchen1'}][{'Blue things One'}][{'Iron'}";
 var_dump($f[$ironKeys]);
 Output:
 NULL
我们能否动态地将密钥分配给$kitchenKeys或/和$dishKeys或/和$ironKeys,以获取多维数组中的任何密钥值?

Chay22是正确的,并且基于

$f['Kitchen']['Dishes']['Mantovarka']=3;
$f['Kitchen']['Dishes']['Castrool']=91;
$f['Kitchen']['Dishes']['Separator']=10;
$f['Kitchen']['Product']=18;
$f['Kitchen']['Textile']=19;
$f['Kitchen']['Blue things']['Juicemaker']=25;
$f['Kitchen']['Blue things']['Freegener']=13;
$f['Kitchen1']['Blue things One']['Microwave']=4;
$f['Kitchen1']['Blue things One']['Iron']=24;
$f['Kitchen1']['Dishes One']['Separator']=110;
这将起作用<代码>评估('var_dump($f['.$dishKeys.];')

您还可以将其作为一个可用的变量,如下所示

eval('$newkey = ($f['.$dishKeys.']);');
var_dump($newkey);

或者,您可以使用一个函数使用点符号访问内部键。例如“key1.key2.key3”。可在此处找到此类示例:


但是上面描述的评估方法也会起作用。但是,由于未设置键,您更容易出错,并且如果您是从用户输入设置动态键,则清理将成为一个问题

我认为我动态地做这件事的方法是获取一个值数组,然后在foreach内部(在函数内部,如果您多次使用它,我是为了显示而做的),为数组的元素生成一个字符串。然后设置一个包含要运行的命令的字符串,最后使用
eval()
执行它。小心这些数组将包含的内容,特别是如果它是用户生成的,因为
eval()
可能会很危险

$dishKeysArray = array("Kitchen", "Dishes");

$evalString = getEvalString($dishKeysArray);

eval($evalString);

function getEvalString($array){
    $elementString = "";
    foreach($array as $arr){
        $elementString .= "['$arr']";
    }
    $evalString = 'var_dump($f' . $elementString . ');';
    return $evalString;
}
希望这有帮助


请注意双引号和单引号的具体用法,因为有时我们希望实际使用数组的值,有时我们希望逐字输出数组名称(
$f

我想您可能需要
eval()
来实现这一点,我想这取决于您所说的动态。您可以轻松地为变量赋值以访问它。e、 g.
$value1=“Kitchen1”$value2=“菜品一”;打印($f[$value1][$value2])谢谢你的建议!我是这样使用的:
$my_array=array(“0”=>“Kitchen1”,“1”=>“蓝色物品一号”);提取($my_数组,EXTR_前缀_ALL,'index')
$dishKeys=“'{$index_0}']['{$index_1}'”;eval('$newkey=($f['.$dishKeys.]);”);var_dump($newkey);