在 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] =>
部分并添加逗号.我知道这一定是一个非常简单的过程,但我无法找到任何演示如何执行此操作的文档.
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.
推荐答案
有 php 中的 foreach 循环.你必须遍历数组.
There is foreach loop in php. You have to traverse the array.
foreach($array as $key => $value)
{
echo $key." has the value". $value;
}
如果您只是想在值之间添加逗号,请考虑使用 implode
If you simply want to add commas between values, consider using implode
$string=implode(",",$array);
echo $string;
这篇关于在 PHP 中显示数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!