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>'
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?
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.
To print properly an array, you either loop through it and echo each element, or you can use 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.
What the PHP Notice means and how to reproduce it:
If you send a PHP array into a function that expects a string like: echo or print, then the PHP interpreter will convert your array to the literal string Array, throw this Notice and keep going. 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 dumps the literal string: Array to stdout and then logs the Notice to stderr and keeps going.
Another example in a PHP script:
<?php
$stuff = array(1,2,3);
print $stuff; //PHP Notice: Array to string conversion in yourfile on line 3
?>
Correction 1: use foreach loop to access array elements
http://php.net/foreach
$stuff = array(1,2,3);
foreach ($stuff as $value) {
echo $value, "\n";
}
Prints:
1
2
3
Or along with array keys
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
foreach ($stuff as $key => $value) {
echo "$key: $value\n";
}
Prints:
name: Joe
email: joe#example.com
Note that array elements could be arrays as well. In this case either use foreach again or access this inner array elements using array syntax, e.g. $row['name']
Correction 2: Joining all the cells in the array together:
In case it's just a plain 1-demensional array, you can simply join all the cells into a string using a delimiter:
<?php
$stuff = array(1,2,3);
print implode(", ", $stuff); //prints 1, 2, 3
print join(',', $stuff); //prints 1,2,3
Correction 3: Stringify an array with complex structure:
In case your array has a complex structure but you need to convert it to a string anyway, then use http://php.net/json_encode
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
print json_encode($stuff);
Prints
{"name":"Joe","email":"joe#example.com"}
A quick peek into array structure: use the builtin php functions
If you want just to inspect the array contents for the debugging purpose, use one of the following functions. Keep in mind that var_dump is most informative of them and thus usually being preferred for the purpose
http://php.net/print_r
http://php.net/var_dump
http://php.net/var_export
examples
$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)
}
You are using <input name='C[]' in your HTML. This creates an array in PHP when the form is sent.
You are using echo $_POST['C']; to echo that array - this will not work, but instead emit that notice and the word "Array".
Depending on what you did with the rest of the code, you should probably use echo $_POST['C'][0];
Array to string conversion in latest versions of php 7.x is error, rather than notice, and prevents further code execution.
Using print, echo on array is not an option anymore.
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 foreach or for to output input data for names that are declared as input arrays ('name[]')
Most common practice to catch errors is using try/catch blocks, that helps us prevent interruption of code execution that might cause possible errors wrapped within try block.
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();
}
<?php
ob_start();
var_dump($_POST['C']);
$result = ob_get_clean();
?>
if you want to capture the result in a variable
You can also try like this:
if(isset($_POST['G'])){
if(isset($_POST['C'])){
for($i=0; $i< count($_POST['C']); $i++){
echo $_POST['C'][$i];
}
}
}
Anybody got a clue what's going on here?
Let's take this very simple piece of code:
$p_id = array();
foreach($opp->participants as $party) {
echo "ID value from data:" . var_dump($party->id) . "<br>";
echo "Base array:" . var_dump($p_id) . "<br>";
$p_uuid = array();
$p_assignment_id = array();
$p_id = array_push($p_id, "$party->id");
echo "Dump array result:" .var_dump($p_id) . "<br>";
}
This is the output I'm getting from this (Yes, that's the formatting of the output too):
int(295) ID value from data:<br>
array(0) { } Base array:<br>
int(1) Dump array result:<br>
int(298) ID value from data:<br>
array(0) { } Base array:<br>
int(1) Dump array result:<br>
int(301) ID value from data:<br>
array(0) { } Base array:<br>
int(1) Dump array result:<br>
This is obviously a noob question, but I honestly have no idea why the output looks like this.
if I use print_r, this is the result I get:
295 ID value from data:1<br>
Array ( ) Base array: 1<br>
1 Dump array result: 1<br>
298 ID value from data:1<br>
Array ( ) Base array: 1<br>
1 Dump array result: 1<br>
301 ID value from data:1<br>
Array ( ) Base array: 1<br>
1 Dump array result: 1<br>
What I'm expecting to see is an array of the ID values from $party->id, so a print_r of $p_id should result something like Array(295, 298, 301).
Instead the result is 1. Not array(1). Just 1.
For even more clarity, the data this is pulling in doesn't even have 1 as an id. So it shouldn't even exist.
I've included the print_r, and var_dump results, so you can see the raw debug output.
You do not need to assign the result of the array_push() to $p_id. As per PHP documentation the return value of an array_push() function is the new number of elements in the array and not the array itself. See here
Change the code to :
array_push($p_id, "$party->id");
That should do it.
I have a 2d array $locations that is the result of a sql query. I can use the foreach function to get all of the rows, like this, and it works perfectly:
foreach($locations as $row) {
echo $row->NICKNAME;
echo $row->POC;
}
I just want to grab the first row of the index NICKNAME in the array. I try
echo $locations["NICKNAME"][0];
and it says "Undefined index: NICKNAME"
I try:
echo $locations[0][0];
and it says "Cannot use object of type stdClass as array"
When I echo gettype($locations) it prints the word array, and the foreach function (which is only for arrays right?) works, so I really don't understand that error.
I know this is simple but I don't know what else to try and Googling hasn't helped.
Try using this as $location is an array of objects and to reference each object, you have to use $location along with the key of the object you want to select. Once selected, use the the Nickname from it as a regular object property.
echo $locations[0]->NICKNAME;
I am trying to read the array from the following code. I thought it should be easy but I am struggling.
My code pulls back two variables in the array.
$stmt5=$mysql_link->prepare("SELECT stationlong AS stationlong,tpl AS tpl FROM station WHERE stationlong=:stationlong LIMIT 1");
$stmt5->execute(array(':stationlong'=>$q));
$stations=$stmt5->fetchAll(PDO::FETCH_KEY_PAIR);
var_dump($stations);
$stationlongs=$stations[0];
$stationshorts=$stations[1];
An example array is as follows:-
array(1) {["Leicester"]=>string(6) "LESTER"}
My error is NOTICE: undefined offset: 0 in........... for $stationlongs=$stations[0]; and the same again for $stationshorts but with offset 1
Key 0 doesn't exist. As you see in dump, you have one record. There are no keys 0 and 1 in your one-item array.
echo $stations['Leicester']; // returns LESTER
i am Reading the file contents and passed it in explod function("=",$string) ,it gives me two array parts[0].parts[1] seprated by = .parts[1] array displays all the values of the variable .now how can i use these values one by one to pass in the text box .The variable value comes in this way (value1
value2
value3
value4...)
my code also throws the undefined offset :1 notice when i prints the parts[1]arrray
if you have array values iterate over the array like
<?
foreach($array1 as $arr)
{
?>
<input type=test value= />
<?
}
?>