我有这个数组:
$firstArray = [
'location', 'address', 'streetNumber'
];
从该字符串变量中提取:
$string = "location.address.streetNumber"
我想在具有这些变量的其他数组中动态获取值:
$value = $secondArray[$firstArray[0]][$firstArray[1]][$firstArray[2]];`
但不直接使用键([0],[1],...)。
有可能吗?
非常感谢!
答案 0 :(得分:1)
这会为你做这项工作吗?
$string = "location.address.streetNumber";
$firstArray = explode('.', $string);
$secondArray = ['location' => ['address' => ['streetNumber' => 'street 854']]];
$ref = &$secondArray;
foreach($firstArray as $val){
$ref = &$ref[$val];
}
$value = $ref;
unset($ref);
echo $value // street 854