php 如何在PHP中将数组转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4260590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert an array to a string in PHP?
提问by shaytac
For an array like the one below; what would be the best way to get the array values and store them as a comma-separated string?
对于像下面这样的数组;获取数组值并将它们存储为逗号分隔的字符串的最佳方法是什么?
Array ( [0] => 33160,
[1] => 33280,
[2] => 33180,
[3] => 33163,
[4] => 33181,
[5] => 33164,
[6] => 33162,
[7] => 33179,
[8] => 33154,
[9] => 33008,
[10] => 33009,
[11] => 33161,
[12] => 33261,
[13] => 33269,
[14] => 33169,
[15] => 33022,
[16] => 33141,
[17] => 33168,
[18] => 33020,
[19] => 33023,
[20] => 33019,
[21] => 33153,
[22] => 33238,
[23] => 33138,
[24] => 33167,
[25] => 33082,)
回答by Teekin
I would turn it into CSV form, like so:
我会将其转换为 CSV 形式,如下所示:
$string_version = implode(',', $original_array)
You can turn it back by doing:
您可以通过执行以下操作将其转回:
$destination_array = explode(',', $string_version)
回答by pixeline
I would turn it into a json object, with the added benefit of keeping the keys if you are using an associative array:
我会把它变成一个 json 对象,如果你使用关联数组,还有保留键的额外好处:
$stringRepresentation= json_encode($arr);
回答by Rocket Hazmat
回答by Isaac Sutherland
serialize()and unserialize()convert between php objects and a string representation.
serialize()和unserialize()在 php 对象和字符串表示之间进行转换。
回答by Vikas_web
回答by Vikas_web
implode is function , which used in php for converting the array into string.
implode 是 function ,它在 php 中用于将数组转换为字符串。
you may use like this
你可以这样使用
$string_product = implode(',',$array)
$string_product = implode(',',$array)
回答by bcosca
implode(' ',$array);