在PHP中显示数组值 [英] Display array values in PHP
问题描述
所以,我用PHP工作的第一次,我试图来检索和显示数组的值。很多谷歌搜索后,唯一的方法我可以找到这个是的print_r
,的var_dump
或 var_export
。然而,所有这些方法返回的东西,看起来像这样:
So, I'm working with PHP for the first time and I am trying to retrieve and display the values of an array. After a lot of googling, the only methods I can find for this are print_r
, var_dump
or var_export
. However, all of these methods return something that looks like this:
[a] => apple
[b] => banana
[c] => orange
我无法弄清楚如何风格这个outout。我需要抛开 [A] =>
部分并添加逗号。我知道这一定是pretty简单的过程,但我一直无法追查演示如何做的文档。
I can't figure out how to style this outout. I need to strip away the [a] =>
part and add commas. I know this must be a pretty straightforward process but I haven't been able to track down any documentation that demonstrates how to do it.
推荐答案
There is foreach loop in php. You have to traverse the array.
foreach($array as $key => $value)
{
echo $key." has the value". $value;
}
如果您只是想值之间添加逗号,请考虑使用破灭
If you simply want to add commas between values, consider using implode
$string=implode(",",$array);
echo $string;
这篇关于在PHP中显示数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!