如何解决PHP错误'注意:数组到字符串转换...'

t4t*_*ina 99 html php

我有一个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>'
Run Code Online (Sandbox Code Playgroud)

这是回应POST的代码.

if(!empty($_POST['G'])){
    echo $_POST['C'];
}
Run Code Online (Sandbox Code Playgroud)

但是当代码运行时,我得到一个错误:

Notice: Array to string conversion in 
C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 8
Run Code Online (Sandbox Code Playgroud)

这个错误意味着什么,我该如何解决?

jad*_*k94 99

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

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

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


Eri*_*ski 48

PHP通知的含义以及如何重现它:

如果你将一个PHP数组发送到一个需要字符串的函数:echo或者print,那么PHP解释器会将你的数组转换为文字字符串Array,抛出这个注意事项并继续.例如:

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
Run Code Online (Sandbox Code Playgroud)

在这种情况下,函数print将文字字符串:转储Array到stdout,然后将通知记录到stderr并继续运行.

PHP脚本中的另一个示例:

<?php
    $stuff = array(1,2,3);
    print $stuff;  //PHP Notice:  Array to string conversion in yourfile on line 3
?>
Run Code Online (Sandbox Code Playgroud)

你有2个选项,要么使用数组到字符串转换器将PHP数组转换为String,要么禁止PHP通知.

更正1:使用内置的php函数print_r或var_dump:

http://php.net/manual/en/function.print-r.phphttp://php.net/manual/en/function.var-dump.php

$stuff = array(1,2,3);
print_r($stuff);
$stuff = array(3,4,5);
var_dump($stuff);
Run Code Online (Sandbox Code Playgroud)

打印:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
array(3) {
  [0]=>
  int(3)
  [1]=>
  int(4)
  [2]=>
  int(5)
}
Run Code Online (Sandbox Code Playgroud)

更正2:使用json_encode将数组折叠为json字符串:

$stuff = array(1,2,3);
print json_encode($stuff);   //Prints [1,2,3]
Run Code Online (Sandbox Code Playgroud)

更正3:将阵列中的所有单元连接在一起:

<?php
    $stuff = array(1,2,3);
    print implode(", ", $stuff);    //prints 1, 2, 3
    print join(',', $stuff);        //prints 1, 2, 3
?>
Run Code Online (Sandbox Code Playgroud)

更正4:压制通知:

error_reporting(0);
print(array(1,2,3));    //Prints 'Array' without a Notice.
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。对我帮助很大。我还必须使用 json_encode 来回答这个问题。print_r ( json_encode ( $AuthData ) ); Result similar to this: [{"system_id":"61a694d0-3605-4502-952b-38d87b451a56","system_auth_id":"caa5906f-d9ae-4297-8e9f-5ea8d9ed8b51","system_lastauth_id":"ace681bb-48f5-4831- a23d-6608c696f264","system_rundate":"2019-04-27T22:46:07.090Z"}] (2认同)

Nik*_*cic 6

Array to string conversion在最新版本的 php 7.x 中,这是错误,而不是通知,并阻止进一步的代码执行。

在数组上使用print,echo不再是一种选择。

抑制错误和通知并不是一个好的做法,尤其是在开发环境中并且仍在调试代码时。

使用var_dump, print_r, 使用foreach或迭代输入值for来输出声明为输入数组 (' name[]')的名称的输入数据

捕获错误的最常见做法是使用try/catch块,这有助于我们防止代码执行中断,从而可能导致try块中包含可能的错误。

  try{  //wrap around possible cause of error or notice
    
    if(!empty($_POST['C'])){
        echo $_POST['C'];
    }

  }catch(Exception $e){

    //handle the error message $e->getMessage();
  }
Run Code Online (Sandbox Code Playgroud)

  • 那将是一个**非常**坏主意。此错误中没有任何需要“处理”的内容。必须立即**修复**。添加 try catch 在这里完全没有意义。 (3认同)

Sve*_*ven 5

您正在使用<input name='C[]'HTML.这在表单发送时在PHP中创建一个数组.

您正在使用echo $_POST['C'];回显该数组 - 这不起作用,而是发出该通知和单词"Array".

根据您对其余代码所做的操作,您应该使用 echo $_POST['C'][0];