P粉9868609502023-08-22 09:46:05
If you pass a PHP array to a function that expects a string, such as echo
or print
, then the PHP interpreter will convert your array to a literal stringArray
, throw this Notice and continue execution. For example:
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 print
outputs the literal string Array
to stdout, then logs the Notice to stderr and continues execution.
Another PHP script example:
<?php $stuff = array(1,2,3); print $stuff; //PHP Notice: Array to string conversion in yourfile on line 3 ?>
$stuff = array(1,2,3); foreach ($stuff as $value) { echo $value, "\n"; }
Output:
1 2 3
Or contain array key name:
$stuff = array('name' => 'Joe', 'email' => 'joe@example.com'); foreach ($stuff as $key => $value) { echo "$key: $value\n"; }
Output:
name: Joe email: joe@example.com
Note that array elements can also be arrays. In this case, you can use foreach
again or use array syntax to access the inner array elements, like $row['name']
If it is just an ordinary one-dimensional array, you can use the delimiter to concatenate all cells into a string:
<?php $stuff = array(1,2,3); print implode(", ", $stuff); //输出 1, 2, 3 print join(',', $stuff); //输出 1,2,3
If your array has a complex structure but still need to convert it to a string, you can use http://php.net/json_encode
$stuff = array('name' => 'Joe', 'email' => 'joe@example.com'); print json_encode($stuff);
Output:
{"name":"Joe","email":"joe@example.com"}
If you just want to inspect the array contents for debugging purposes, you can use one of the following functions. Keep in mind that var_dump is the most verbose of them all and is usually preferred for this purpose
Example:
$stuff = array(1,2,3); print_r($stuff); $stuff = array(3,4,5); var_dump($stuff);
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 ) array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) }
P粉7398862902023-08-22 00:07:45
When you have a lot of HTML input named C[]
, what you get on the other end of the POST array is an array of those values $_POST['C']
. So when you echo
it, you are trying to print an array, so it will just print Array
and a hint.
To properly print an array, you can loop through it and echo
each element, or you can use print_r
.
Also, if you don't know if it's an array or a string or whatever, you can use var_dump($var)
and it will tell you what type it is and what its contents are . For debugging purposes only.