在 PHP 中显示数组值

发布于 2024-11-01 10:46:18 字数 322 浏览 5 评论 0原文

因此,我是第一次使用 PHP,并且尝试检索并显示数组的值。经过大量谷歌搜索后,我能找到的唯一方法是 print_rvar_dumpvar_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

甜嗑 2024-11-08 10:46:18

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

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

如果您只想在值之间添加逗号,请考虑使用 implode

$string=implode(",",$array);
echo $string;

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;
庆幸我还是我 2024-11-08 10:46:18

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

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

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

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

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

echo $withComma;
// Will display apple,banana,orange
一生独一 2024-11-08 10:46:18
<?php $data = array('a'=>'apple','b'=>'banana','c'=>'orange');?>
<pre><?php print_r($data); ?></pre>

结果:
数组

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


抱歉,我读得太快并且误解了上面的内容。

<?php
$fruites = array('apple', 'banana', 'orange');
echo implode(',',$fruites);
?>

结果:
苹果、香蕉、橙子

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

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


Sorry for the above, I read it too quickly and misunderstood.

<?php
$fruites = array('apple', 'banana', 'orange');
echo implode(',',$fruites);
?>

Result:
apple,banana,orange

恋竹姑娘 2024-11-08 10:46:18

join() 函数必须适合您:

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

输出 :

苹果、香蕉、凤梨

the join() function must work for you:

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

Output :

apple,banana,ananas

玩世 2024-11-08 10:46:18

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

$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);

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);
缱倦旧时光 2024-11-08 10:46:18

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

或者

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

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

Or

foreach($array as $key => $value)
{
   echo $key." is ". $value;
}
自控 2024-11-08 10:46:18

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

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

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

foreach ($array as $key => $value) {
    echo $key . ' contains ' . $value . '<br/>';
}
ゞ记忆︶ㄣ 2024-11-08 10:46:18

您可以轻松地使用 join()

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

you can easily use join()

$fruits = array("apple", "banana", "orange");
print join(" ".$fruits);
云淡月浅 2024-11-08 10:46:18

其他选项:

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

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>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文