PHP 数组到字符串的等效项
我想知道是否有人有将数组转换为字符串的递归解决方案。
我的意思是:
一个包含以下内容的数组 $args
:
Array
(
[0] => $hello
[1] => 411px
[Jeeves] => Array
(
[compiling] => 1
)
)
调用 arr_to_string($args)
后的结果:
array($hello,"411px", "Jeeves" => array("compiling" => 1));
注意: 它识别前面的 $ 符号,因此不会添加引号。对于数字也有同样的作用。
有人有任何解决方案或可以指出我正确的方向吗?
谢谢! 马特·穆勒
I'm wondering if anyone has a recursive solution to converting an array to a string.
Here's what I mean:
An array $args
that has the following contents:
Array
(
[0] => $hello
[1] => 411px
[Jeeves] => Array
(
[compiling] => 1
)
)
Result after calling arr_to_string($args)
:
array($hello,"411px", "Jeeves" => array("compiling" => 1));
Note:
It recognizes the $ sign in front and therefore does not add quotes. It does the same for numbers.
Anyone have any solution or can point me in the right direction?
Thanks!
Matt Mueller
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在寻找
var_export
— 输出或返回变量的可解析字符串表示形式,但不会给出 $hello,因为 $hello 不能位于数组中。它始终只是变量的值,而不是变量名称。如果您想要'$hello',请在将其插入数组时将其放入单引号中,例如将其作为字符串插入,而不是作为变量插入。
Looks like you are after
var_export
— Outputs or returns a parsable string representation of a variableThat won't give you $hello though, because $hello cannot be in an array. It's always just the value of the variable, not the variable name. If you want '$hello', put it into single quotes when inserting it to the array, e.g. insert it as a string, not as a variable.