通过键字符串获取数组值
我正在为我的下一个项目构建一个模板引擎,进展顺利。它将 {tag}
替换为相应的值。
我也希望替换 {tag[0][key]}
。我需要知道的是如何获取值,如果我有数组和键的字符串表示形式,如下所示:
$arr = array(
0 => array(
'key' => 'value'
),
1 => array(
'key' => 'value2'
)
);
$tag = 'arr[0][key]';
echo($$tag);
这是问题的一个非常简单的版本,我希望你理解它。否则我很乐意回答有关它的任何问题。
I'm building a template engine for my next project, which is going great. It replaces {tag}
with a corresponding value.
I want {tag[0][key]}
to be replaced as well. All I need to know is how to get the value, if I have the string representation of the array and key, like this:
$arr = array(
0 => array(
'key' => 'value'
),
1 => array(
'key' => 'value2'
)
);
$tag = 'arr[0][key]';
echo($tag);
This is a very simple version of the problem, I hope you understand it. Or else I would be happy to answer any questions about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意没有必要重新发明轮子:PHP 本身就是作为模板引擎诞生的,并且仍然擅长这样做:
它甚至曾经拥有现在已弃用的形式
无论如何,您都可以执行以下操作
I agree that there is no need to reinvent the wheel: PHP itself was born as a template engine, and is still good at doing this:
It even used to have the now deprecated form
In any case you could do the following