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
How to comma separate the values of an array for display
提问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 $array1
to display:
“想法”是让 echo$array1
显示:
1,2,3
1,2,3
from the second example above.
从上面的第二个例子。
回答by Hoatzin
http://us.php.net/manual/en/function.implode.php
http://us.php.net/manual/en/function.implode.php
echo implode(", ", $array);
回答by John Parker
回答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 implode
function as well.
注意 - 您也可以使用implode
函数。