问题描述
假设这个数组:
$array = [
'name' => 'any Name','settings' => [
'app' => 'Spy','location' => 'Earth','locale' => 'en'
]
];
我想获得 app
值
echo $array['settings']['app'];
但是这种方法不起作用,因为我无法在我的情况下写括号 [] []
我想要一个类似这样的方法:
$handle = 'app,name';
echo $array[$handle] ;
或
$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