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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:26:21  来源:igfitidea点击:

How to convert an array to a string in PHP?

phparrays

提问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

Using implode(), you can turn the array into a string.

使用implode(),您可以将数组转换为字符串。

$str = implode(',', $array); // 33160,33280,33180,...

回答by Isaac Sutherland

serialize()and unserialize()convert between php objects and a string representation.

serialize()unserialize()在 php 对象和字符串表示之间进行转换。

回答by Vikas_web

PHP has a built-in function implodeto assign array values to string. Use it like this:

PHP 有一个内置函数implode可以将数组值分配给字符串。像这样使用它:

$str = implode(",", $array);

回答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 cdhowie

回答by bcosca

implode(' ',$array);