可能重复:
PHP:如何使用字符串作为数组索引路径来检索值
如果我有每个元素的路径字符串,如何设置或获取数组的值?
例如,当
PHP:如何使用字符串作为数组索引路径来检索值
我有一个数组,如下所示:
$array['image']['comment'] = 'something';
$array['image']['tag'] = 'happy';
$array['image']['colors']['blue'] = '12345';
如果我有每个元素的路径字符串,如何设置或获取数组的值?
例如,当
$path = 'image/colors/blue';
时,下面的函数应该返回 12345
。function get_array($array, $path)
{
//what goes here?
}
function set_array($array, $path, $value)
{
//what goes here?
}