如何解决 PHP 错误“注意:数组到字符串的转换...”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20017409/
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
How to solve PHP error 'Notice: Array to string conversion in...'
提问by t4thilina
I have a PHP file that tries to echo a $_POSTand I get an error, here is the code:
我有一个尝试回显 a 的 PHP 文件,$_POST但出现错误,这是代码:
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>'
Here is the code to echo the POST.
这是回显 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?
这个错误是什么意思,我该如何解决?
采纳答案by jadkik94
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 echothat, you are trying to print an array, so all it does is print Arrayand a notice.
当您有许多 HTML 输入命名时C[],您在另一端的 POST 数组中得到的是这些值的数组$_POST['C']。因此,当您这样做时echo,您正在尝试打印一个数组,因此它所做的只是打印Array和通知。
To print properly an array, you either loop through it and echoeach element, or you can use print_r.
要正确打印数组,您可以遍历它和echo每个元素,也可以使用print_r.
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.
或者,如果您不知道它是数组还是字符串或其他什么,您可以使用var_dump($var)which 来告诉您它是什么类型以及它的内容是什么。仅将其用于调试目的。
回答by Eric Leschinski
What the PHP Notice means and how to reproduce it:
PHP 通知的含义以及如何重现它:
If you send a PHP array into a function that expects a string like: echoor print, then the PHP interpreter will convert your array to the literal string Array, throw this Notice and keep going. For example:
如果你将一个 PHP 数组发送到一个需要字符串的函数中,例如: echoor print,那么 PHP 解释器会将你的数组转换为文字 string Array,抛出这个 Notice 并继续。例如:
php> print(array(1,2,3))
PHP Notice: Array to string conversion in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(591) :
eval()'d code on line 1
Array
In this case, the function printdumps the literal string: Arrayto stdout and then logs the Notice to stderr and keeps going.
在这种情况下,该函数print将文字字符串:转储Array到 stdout,然后将通知记录到 stderr 并继续运行。
Another example in a PHP script:
PHP 脚本中的另一个示例:
<?php
$stuff = array(1,2,3);
print $stuff; //PHP Notice: Array to string conversion in yourfile on line 3
?>
You have 2 options, either cast your PHP array to String using an array to string converter or suppress the PHP Notice.
您有 2 个选项,要么使用数组到字符串转换器将 PHP 数组转换为字符串,要么取消 PHP 通知。
Correction 1: use the builtin php function print_r or var_dump:
更正1:使用内置的php函数print_r或var_dump:
http://php.net/manual/en/function.print-r.phpor http://php.net/manual/en/function.var-dump.php
http://php.net/manual/en/function.print-r.php或http://php.net/manual/en/function.var-dump.php
$stuff = array(1,2,3);
print_r($stuff);
$stuff = array(3,4,5);
var_dump($stuff);
Prints:
印刷:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
Correction 2: Use json_encode to collapse the array to json string:
更正 2:使用 json_encode 将数组折叠为 json 字符串:
$stuff = array(1,2,3);
print json_encode($stuff); //Prints [1,2,3]
Correction 3: Joining all the cells in the array together:
更正 3:将数组中的所有单元格连接在一起:
<?php
$stuff = array(1,2,3);
print implode(", ", $stuff); //prints 1, 2, 3
print join(',', $stuff); //prints 1, 2, 3
?>
Correction 4: suppress the Notices:
更正4:压制公告:
error_reporting(0);
print(array(1,2,3)); //Prints 'Array' without a Notice.
回答by Sven
You are using <input name='C[]'in your HTML. This creates an array in PHP when the form is sent.
您正在<input name='C[]'HTML中使用。这会在发送表单时在 PHP 中创建一个数组。
You are using echo $_POST['C'];to echo that array - this will not work, but instead emit that notice and the word "Array".
您正在使用echo $_POST['C'];回显该数组 - 这将不起作用,而是发出该通知和“数组”一词。
Depending on what you did with the rest of the code, you should probably use echo $_POST['C'][0];
根据您对其余代码所做的工作,您可能应该使用 echo $_POST['C'][0];
回答by Nikola Kirincic
Array to string conversionin latest versions of php 7.x is error, rather than notice, and prevents further code execution.
Array to string conversion在最新版本的 php 7.x 中是错误,而不是通知,并阻止进一步的代码执行。
Using print, echoon array is not an option anymore.
在阵列上使用print,echo不再是一种选择。
Suppressing errors and notices is not a good practice, especially when in development environment and still debugging code.
抑制错误和通知不是一个好习惯,尤其是在开发环境中并且仍在调试代码时。
Use var_dump,print_r, iterate through input value using foreachor forto output input data for names that are declared as input arrays ('name[]')
使用var_dump, print_r, 迭代输入值,使用foreach或for输出声明为输入数组(' name[]')的名称的输入数据
Most common practice to catch errors is using try/catchblocks, that helps us prevent interruption of code execution that might cause possible errors wrapped within tryblock.
捕获错误的最常见做法是使用try/catch块,这有助于我们防止代码执行中断,这可能会导致可能的错误包裹在try块中。
try{ //wrap around possible cause of error or notice
if(!empty($_POST['G'])){
echo $_POST['C'];
}
}catch(Exception $e){
//handle the error message $e->getMessage();
}
回答by Andrei Dragomir
<?php
ob_start();
var_dump($_POST['C']);
$result = ob_get_clean();
?>
if you want to capture the result in a variable
如果你想在一个变量中捕获结果

