在 PHP 中显示数组值

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

Display array values in PHP

phparraysassociative-array

提问by Thomas

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_dumpor var_export. However, all of these methods return something that looks like this:

所以,我是第一次使用 PHP,我正在尝试检索和显示数组的值。经过大量的谷歌搜索,我能找到的唯一方法是print_r,var_dumpvar_export。但是,所有这些方法都返回如下所示的内容:

[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.

我不知道如何设计这个outout。我需要去掉[a] =>部分并添加逗号。我知道这一定是一个非常简单的过程,但我无法找到任何演示如何执行此操作的文档。

回答by Shakti Singh

There is foreachloop in php. You have to traverse the array.

php中有foreach循环。你必须遍历数组。

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;

回答by j_freyre

You can use implode to return your array with a string separator.

您可以使用内爆返回带有字符串分隔符的数组。

$withComma = implode(",", $array);

echo $withComma;
// Will display apple,banana,orange

回答by DJ.Atomix

<?php $data = array('a'=>'apple','b'=>'banana','c'=>'orange');?>
<pre><?php print_r($data); ?></pre>

Result:
Array
(
          [a] => apple
          [b] => banana
          [c] => orange
)

结果:
数组

          [a] => 苹果
          [b] => 香蕉
          [c] => 橙

回答by Gaurav

use implode(',', $array);for output as apple,banana,orange

使用implode(',', $array);了作为输出apple,banana,orange

Or

或者

foreach($array as $key => $value)
{
   echo $key." is ". $value;
}

回答by JazZ

the join()function must work for you:

join()功能必须适合您:

$array = array('apple','banana','ananas');
$string = join(',', $array);
echo $string;

Output :

输出 :

apple,banana,ananas

苹果、香蕉、香蕉

回答by Kerem Cavusoglu

a simple code snippet that i prepared, hope it will be usefull for you;

我准备的一个简单的代码片段,希望对你有用;

$ages = array("Kerem"=>"35","Ahmet"=>"65","Talip"=>"62","Kamil"=>"60");

reset($ages);

for ($i=0; $i < count($ages); $i++){
echo "Key : " . key($ages) . " Value : " . current($ages) . "<br>";
next($ages);
}
reset($ages);

回答by Stefan Gehrig

Iterate over the array and do whatever you want with the individual values.

遍历数组并对各个值执行任何您想要的操作。

foreach ($array as $key => $value) {
    echo $key . ' contains ' . $value . '<br/>';
}

回答by user3243940

you can easily use join()

您可以轻松使用 join()

$fruits = array("apple", "banana", "orange");
print join(" ".$fruits);

回答by Meindert A Jorna dewebmeester

Other option:

其他选择:

$lijst=array(6,4,7,2,1,8,9,5,0,3);
for($i=0;$i<10;$i++){
echo $lijst[$i];
echo "<br>";
}