使用数组或字符串从数组调用值-php

使用数组或字符串从数组调用值-php,php,arrays,Php,Arrays,我在编写代码时遇到了一个问题,在代码中我可以得到值,但是使用文本或数组 假设此数组: $array = [ 'name' => 'any Name', 'settings' => [ 'app' => 'Spy', 'location' => 'Earth', 'locale' => 'en' ] ]; 我想得到app值 默认代码: echo$array['settin

我在编写代码时遇到了一个问题,在代码中我可以得到值,但是使用文本或数组

假设此数组:

$array = [
    'name' => 'any Name',
    'settings' => [
            'app' => 'Spy',
            'location' => 'Earth',
            'locale' => 'en'
    ]
];
我想得到
app

默认代码:

echo$array['settings']['app']

但是这个方法不起作用,因为在我的例子中我不能写括号[]

我想要一个类似的方法: 或 我知道代码是错误的,但是举个例子


我希望这个想法已经出现,并且有一个解决方案。

会是这样吗

<?php

$array = [
    'name' => 'any Name',
    'settings' => [
            'app' => 'Spy',
            'location' => 'Earth',
            'locale' => 'en'
    ]
];

$str = 'settings|app'; // this is string you have

foreach (explode('|', $str) as $key) {
    if (!isset($output)) {
        $output = $array[$key];
    } else {
        $output = $output[$key];    
    }
}

echo $output; // spy

这是不可能的。你宁愿玩参考资料。@nice\u dev你知道什么主意吗?为什么不使用标准方法?基本PHP语法
echo$array['settings']['app']
$handle=…'短;echo$array[$handle]
…@pavel我有一个函数,可以通过键入以下关键字名来获取数据:get('settings | app')this get$array['settings']['app'];谢谢,伙计work@Hothaifajaber:它应在嵌套级别独立工作-3D数组的字符串
xxx | yyy | zzz
应返回正确的值。你可以试试。
$handle = ['settings']['app'];

echo $array[$handle];
<?php

$array = [
    'name' => 'any Name',
    'settings' => [
            'app' => 'Spy',
            'location' => 'Earth',
            'locale' => 'en'
    ]
];

$str = 'settings|app'; // this is string you have

foreach (explode('|', $str) as $key) {
    if (!isset($output)) {
        $output = $array[$key];
    } else {
        $output = $output[$key];    
    }
}

echo $output; // spy