0

所以我有以下用于注册表单的 PHP 代码:

 <?php
$entries = array(
0 => $_POST['signup_username'],
1 => $_POST['signup_email'],
2 => $_POST['signup_password']);

$entries_unique = array_unique($entries);
$entries_unique_values = array_values($entries_unique);

echo " <br />".$entries_unique_values. " ";
?>

...而且我意识到我的 echo 语法是错误的。如何在不为每个键分配变量的情况下回显数组的不同值(为什么我不能这样做有很多原因)?我也不想使用 r_print 函数。

提前致谢!

4

4 回答 4

0

看看 php 的var_export().

var_export — 输出或返回变量的可解析字符串表示

于 2013-03-30T02:30:30.307 回答
0

试试这个implode()功能:

echo implode(', ', array_values($entries));
于 2013-03-30T02:30:37.340 回答
0

你想如何输出它们?逗号分隔?每个人都有自己的路线?你有很多选择。这应该可以解决逗号分隔的问题:

echo " <br />".implode(', ', $entries_unique). " ";

That said, be careful just outputting user input directly in HTML. This will leave you wide open to XSS vulnerabilities and invalid HTML in general. To output user input in HTML safely, you need to properly HTML encode the output. This would be preferable to the line above:

echo " <br />".implode(', ', array_map('htmlspecialchars', $entries_unique)). " ";

See implode(), array_map(), and htmlspecialchars().

于 2013-03-30T02:33:36.400 回答
0

he foreach loop is really easy for arrays, especially single associate arrays.

foreach($entries_unique as $key => $value) {

echo "key: " . $key . " - value: " . $value . "<br/>";

}

Check out php.net: http://php.net/manual/en/control-structures.foreach.php

于 2013-03-30T02:33:54.857 回答