How to solve PHP error 'Notice: Array to string conversion in...' - php

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];
}
}
}

Related

Array to string conversion ecommerce [duplicate]

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];
}
}
}

There's something wrong with my SQL/PHP prepared statement [duplicate]

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];
}
}
}

Array to string conversion for calculate discount (not resolved in similar question) [duplicate]

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];
}
}
}

How to break a file array into string when calling a function [duplicate]

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];
}
}
}

Cleaner way to echo multi dimension array

echo "{$line['text_1']}";
the above echo works fine ,however when it comes to 2d array, in my sublime, only {$line['text_2']} this part work fine. output error both sublime and browser
echo "$array_2d[{$line['text_1']}][{$line['text_2']}]";
any idea?
update
echo "$array_2d[$line['text_1']][$line['text_2']]";
using xampp, error Parse error: syntax error, unexpected '[', expecting ']' in C:\xampp\htdocs
and I'm just outputting a value from the mysql_fetch_assoc. I can do it in another way by echo '' however I'm trying to make my code easier for future editting and code copy paste
and yes I'm doing things like
echo "The price is $array_2d[$line['text_1']][$line['text_2']]"
with lots of html code in the double quote.
Why are you trying to output the array?
if it is for debugging purposes, you can just use the native php functions print_r() or var_dump()
You should be able to say
echo "item is {$array_2d[$line['text1']][$line['text2']]}";
to get to a subelement.
Of course, this is only really useful when it's not the only thing in the string. If you're only echoing the one value, you don't need the quotes, and things get simpler.
echo $array_2d[$line['text1']][$line['text2']];
this should work :
echo $array_2d[$line['text_1']][$line['text_2']];
When echoing variables, you don't have to use the quotes:
echo $array_2d[$line['text_1']][$line['text_2']];
If you do need to output something with that string, the concatentation operator can help you:
echo "Array: " . echo $array_2d[$line['text_1']][$line['text_2']];
You can use print_r() to echo the array.
e.g.:
print_r($array);
Output will be:
Array ( [test] => 1 [test2] => 2 [multi] => Array ( [multi] => 1 [multi2] => 2 ) )
Also you can use this to make it more readable in a HTML context:
echo '<pre>';
print_r($array);
echo '</pre>';
Output will be:
Array
(
[test] => 1
[test2] => 2
[multi] => Array
(
[multi] => 1
[multi2] => 2
)
)
You can use print_r() or var_dump() to echo an array.
The print_r() displays information about a variable in a way that's readable by humans whereas the var_dump() function displays structured information about variables/expressions including its type and value.
$array = 'YOUR ARRAY';
echo "<pre>";
print_r($array);
echo "</pre>";
or
$array = 'YOUR ARRAY';
var_dump($array);
Example variations
I'm wondering why you would try using the $line array as a key to access data in $array_2d.
Anyway, try this:
echo($line['text_1'].'<br>');
this:
echo($array_2d['text_1']['text_2'].'<br>');
and finally this (based on your "the $line array provides the keys for the $array_2d" array example)
$key_a = $line['text_1'];
$key_b = $line['text_2'];
echo($array_2d[$key_a][$key_b].'<br>');
Which can also be written shorter like this:
echo($array_2d[$line['text_1']][$line['text_2']].'<br>');
Verifying/Dumping the array contents
To verify if your arrays hold the data you expect, do not use print_r. Do use var_dump instead as it will return more information you can use to check on any issues you think you might be having.
Example:
echo('<pre>');
var_dump($array_2d);
echo('</pre>');
Differences between var_dump and print_r
The var_dump function displays structured information of a variable (or expression), including its type and value. Arrays are explored recursively with values indented to show structure. var_dump also shows which array values and object properties are references.
print_r on the other hand displays information about a variable in a readable way and array values will be presented in a format that shows keys and elements. But you'll miss out on the details var_dump provides.
Example:
$array = array('test', 1, array('two', 'more'));
output of print_r:
Array
(
[0] => test
[1] => 1
[2] => Array
(
[0] => two
[1] => more
)
)
output of var_dump:
array(3) {
[0]=> string(4) "test"
[1]=> int(1)
[2]=> array(2)
{
[0]=> string(3) "two"
[1]=> string(4) "more"
}
}

Categories