将关联数组转换为由键命名的单个变量
我有以下格式的数组。我想将其转换为另一种格式,如下所示。
Array
(
[b2] => 2
[b3] => 8
[b4] => 2
[b5] => 4
)
是否可以将 key 转换为 PHP 变量并将其值自动分配给变量?是否可以?
$b2 = 2;
$b3 = 8;
$b4 = 2;
$b5 = 4;
I have array in following format. I want to convert it into another format like below.
Array
(
[b2] => 2
[b3] => 8
[b4] => 2
[b5] => 4
)
Is it possible key is converted into PHP variables and its value automatically assigned to variables? Is it possible?
$b2 = 2;
$b3 = 8;
$b4 = 2;
$b5 = 4;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来你想要 extract()
looks like you want extract()
您正在寻找 extract() 函数。您将要提取的数组传递到当前作用域中,仅此而已。请注意,使用此功能时应非常小心;为了安全起见,请传递
EXTR_SKIP
作为第二个参数,以防止覆盖现有变量,毕竟您不会想要$_SESSION
或$_SERVER
之类的变量代码> 被包含恶意数据的数组限制...You're looking for the extract() function. You pass in the array you want to extract into the current scope, and that's it. Mind though that you should be very careful when you use this function; for safety, pass
EXTR_SKIP
in as the second argument to prevent overwriting existing variables, after all you wouldn't want the likes of$_SESSION
or$_SERVER
throttled by an array with malicious data...