在 PHP 中显示数组值
因此,我是第一次使用 PHP,并且尝试检索并显示数组的值。经过大量谷歌搜索后,我能找到的唯一方法是 print_r
、var_dump
或 var_export
。然而,所有这些方法都会返回如下所示的内容:
[a] => apple
[b] => banana
[c] => orange
我不知道如何设计此输出的样式。我需要去掉 [a] =>
部分并添加逗号。我知道这一定是一个非常简单的过程,但我无法找到任何演示如何执行此操作的文档。
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
php中有 foreach 循环。你必须遍历数组。
如果您只想在值之间添加逗号,请考虑使用 implode
There is foreach loop in php. You have to traverse the array.
If you simply want to add commas between values, consider using implode
您可以使用 implode 返回带有字符串分隔符的数组。
You can use implode to return your array with a string separator.
结果:
数组
(
[a] =>苹果
[b] =>香蕉
[c] =>橙色
)
抱歉,我读得太快并且误解了上面的内容。
结果:
苹果、香蕉、橙子
Result:
Array
(
[a] => apple
[b] => banana
[c] => orange
)
Sorry for the above, I read it too quickly and misunderstood.
Result:
apple,banana,orange
join()
函数必须适合您:输出 :
the
join()
function must work for you:Output :
我准备的一个简单的代码片段,希望对您有用;
a simple code snippet that i prepared, hope it will be usefull for you;
使用
implode(',', $array);
输出为apple,banana,orange
或者
use
implode(',', $array);
for output asapple,banana,orange
Or
迭代数组并对各个值执行任何您想要的操作。
Iterate over the array and do whatever you want with the individual values.
您可以轻松地使用 join()
you can easily use join()
其他选项:
Other option: