php 变量到数组 - 与“提取”相反
PHP 有一个函数 extract 可以将数组转换为:
$array = array(
'var1' => 1,
'var2' => 2
);
到:
$var1 = 1;
$var2 = 2;
现在,我需要相反的,我有几个变量:
$var3 = 'test';
$test = 'another';
$datax = 1;
需要是:
$array = array(
'var3' => 'test',
'test' => 'another',
'datax' => 1
);
PHP 中有类似的东西吗?
PHP has a function extract that will convert an array like this:
$array = array(
'var1' => 1,
'var2' => 2
);
to:
$var1 = 1;
$var2 = 2;
now, I need the opposite, i have few variables:
$var3 = 'test';
$test = 'another';
$datax = 1;
that needs to be:
$array = array(
'var3' => 'test',
'test' => 'another',
'datax' => 1
);
Is there something like this in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用compact()来实现这一点。
参考:http://php.net/manual/en/function.compact.php
You can use
compact()
to achieve this.Reference: http://php.net/manual/en/function.compact.php
像这样
like this
请参阅get_define_vars()
See get_defined_vars()
您必须真的确定您想要执行此操作(它自动包含全局范围内的内容),但您可以使用
如果您希望它比这更有选择性,您可以考虑像这样过滤它这:
You'd have to be really sure you wanted to do this (it includes things in the global scope automatically) but you can use
If you want it more selective than that, you could look at filtering it like this: