如何解决 PHP 错误“注意:数组到字符串的转换..." [英] How to solve PHP error 'Notice: Array to string conversion in...'

查看:24
本文介绍了如何解决 PHP 错误“注意:数组到字符串的转换..."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP 文件试图回显 $_POST 并且出现错误,代码如下:

I have a PHP file that tries to echo a $_POST and I get an error, here is the code:

echo "<html>";
echo "<body>";
for($i=0; $i<5;$i++){
    echo "<input name='C[]' value='$Texting[$i]' " . 
         "style='background-color:#D0A9F5;'></input>";

}
echo "</body>";
echo "</html>";
echo '<input type="submit" value="Save The Table" name="G"></input>'

这是回显 POST 的代码.

Here is the code to echo the POST.

if(!empty($_POST['G'])){
    echo $_POST['C'];
}

但是当代码运行时,我收到如下错误:

But when the code runs I get an error like:

Notice: Array to string conversion in 
C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 8

这个错误是什么意思,我该如何解决?

What does this error mean and how do I fix it?

推荐答案

当你有许多名为 C[] 的 HTML 输入时,你在另一端的 POST 数组中得到的是这些的数组$_POST['C'] 中的值.所以当你 echo 时,你正在尝试打印一个数组,所以它所做的只是打印 Array 和一个通知.

When you have many HTML inputs named C[] what you get in the POST array on the other end is an array of these values in $_POST['C']. So when you echo that, you are trying to print an array, so all it does is print Array and a notice.

要正确打印数组,您可以遍历它并echo每个元素,或者您可以使用print_r.

To print properly an array, you either loop through it and echo each element, or you can use print_r.

或者,如果您不知道它是数组还是字符串或其他什么,您可以使用 var_dump($var) 它会告诉您它是什么类型以及它的内容是什么.仅将其用于调试目的.

Alternatively, if you don't know if it's an array or a string or whatever, you can use var_dump($var) which will tell you what type it is and what it's content is. Use that for debugging purposes only.

这篇关于如何解决 PHP 错误“注意:数组到字符串的转换..."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆