php 如何用逗号分隔数组的值以供显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4762972/
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 14:17:01  来源:igfitidea点击:

How to comma separate the values of an array for display

phparrayslistsplit

提问by JM4

I have searched the PHP.net site and originally thought of some use for the list()function but doesn't seem to accomplish the goal:

我搜索了 PHP.net 站点,最初想到了该list()函数的一些用途,但似乎没有实现目标:

I have an unknown number of values stored in a single array

我在单个数组中存储了未知数量的值

$array1 = array(1,2,3,4,5);

$array1 = array(1,2,3,4,5);

or

或者

$array1 = array(1,2,3);

$array1 = array(1,2,3);

I want to be able to echo (or print_r) the values contained within an array to screen and separated only by commas and spacing.

我希望能够回显(或print_r)包含在数组中的值进行筛选并仅用逗号和空格分隔。

For example:

例如:

the 'idea' is to have echo $array1to display:

“想法”是让 echo$array1显示:

1,2,3

1,2,3

from the second example above.

从上面的第二个例子。

回答by John Parker

You can simply use PHP's implodefunction for this purpose as follows:

为此,您可以简单地使用 PHP 的implode函数,如下所示:

$string = implode(',', array(1,2,3,4,5));

回答by Vinit Kadkol

You can make use of list()function if you know there are only 3 values.

list()如果您知道只有 3 个值,则可以使用函数。

$array = array('Life', 'living', 'Health');
list($life, $living, $health) = $array;

echo $life;
echo $living;
echo $health;

Note - you can make use of implodefunction as well.

注意 - 您也可以使用implode函数。