替换变量中可能的常量
我设置了很多常量。 我有来自数据库的短语,其中可能包含这些常量名称。
我想用常量的值替换常量名称。
Constants:
Array
(
[WORK1] => Pizza Delivery
[WORK2] => Chauffer
[WORK3] => Package Delivery
)
变量:
$variable[0] = "I like doing WORK1";
$variable[1] = "Nothing here move along";
$variable[2] = "WORK3 has still not shown up.";
我如何获得那些具有正确常量值的变量?常量和变量的顺序可以是未排序的。
I have loads of constants set.
I have phrases from database that might have those constant names in them.
I want to replace the constants names with their values.
Constants:
Array
(
[WORK1] => Pizza Delivery
[WORK2] => Chauffer
[WORK3] => Package Delivery
)
Variables:
$variable[0] = "I like doing WORK1";
$variable[1] = "Nothing here move along";
$variable[2] = "WORK3 has still not shown up.";
How would I get those variables with the right constant vales on them? Constants order could be unsorted as well as variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该简单如下:
请注意,在循环之前执行类似的操作可能更理想:
然后直接使用它们,而不是在每次迭代期间调用 array_key/vals。
再想一想,这可能是最好的:
因为它不会从头到尾处理,并且无论常量如何排序,您都应该获得一致的行为。
Should be as simple as:
Note that it is probably more optimal to do something like this before the loop:
And then use them directly instead of calling array_key/vals during every iteration.
And on second thought, this is probably best:
because it doesn't process first to last, and you should get consistent behavior regardless of how the constants are sorted.