Online Tutorials & Training Materials | STechies.com
Register Login

Notice: Array to string conversion in PHP

30 Mar 2020 1:03 pm || 0

In this article, we will take a look at the Array to string conversion error in PHP and try to solve it. This error comes when we try to print array variable as a string using the built-in PHP function print() or echo.

Example

// Declare a PHP array
$myarray = array(1,2,3,4,5,6,7,7);
// Print PHP array using echo and print() functions.
echo $myarray;
print($myarray);

Output

Notice: Array to string conversion in \phpprint.php on line 8
Array
Notice: Array to string conversion in \phpprint.php on line 9
Array

The error is encountered here as the code tries to print the array called myarray like a string. As the echo and print statement is used for printing strings values and scalar values, so when they are not able to treat an array variable as a string.    

Solution

The easiest solution to this problem is to mention the index values along with the echo and print statement. When you type $myarray[2] or $myarray[3], echo and print will be able to recognize it as an array and then display the values.

For example,

$myarray = array(1,2,3,4,5,6,7,7);
// Print PHP array using echo and print() functions.
echo $myarray[2];
print($myarray[0]);   

 The five ways to solve this error are as follows:

  1. Using Builtin PHP Function print_r
  2. Using Built-in PHP Function var_dump
  3. Using PHP Inbuilt Function implode() to Convert Array to String
  4. Using Foreach Loop to Print Element of an Array
  5. Using the json_encode Method

Solution 1: Using Builtin PHP Function print_r

// Declare a PHP array
$myarray = array(1,2,3,4,5,6,7,7);
// Print PHP array using print_r() functions.
print_r($myarray);

Output

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 7 )

Here, the print_r method takes the parameter $myarray and prints its values in the form of keys and values. The keys here are the indexes and the values are the elements in those index positions.   

Solution 2: Using Built-in PHP Function var_dump

// Declare a PHP array
$myarray = array(1,2,3,4,5,6,7,7);
// Print PHP array using print_r() functions.
var_dump($myarray);

Output

array(8) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(7) }

Here, the var_dump method takes the parameter $myarray and prints out the type and the structured information about that variable. It first displays the type of the variable, which is an array in this case. Then it prints the elements in the form of key and value pairs as shown below.   

Solution 3: Using PHP Inbuilt Function implode() to Convert Array to String

// Declare a PHP array
$myarray = array(1,2,3,4,5,6,7,7);
// Convert array to string by using implode function.
$arraydata = implode(',',$myarray);
echo $arraydata;

Output

1,2,3,4,5,6,7,7

In this code, the implode() method is used. This method returns a string after accepting an array of elements as input. Here, the method is passed ‘,’ as the separator and $myarray as the array argument. So, the method takes the array and converts its elements into a string and joins them using a ‘,’ separator.  

Solution 4: Using Foreach Loop to Print Element of an Array

// Declare a PHP array
$myarray = array(1,2,3,4,5,6,7,7);
// run foreach loop to every element of array.
foreach($myarray as $ma){
echo $ma . '<br>';
}

Output

1

2

3

4

5

6

7

7

Here, the foreach loop is used for iterating over the array of elements in $myarray. Then the elements are printed out one by one with a line break after every element, specified by <br>

Solution 5: Using the json_encode Method

The json_encode method takes a JSON encoded string and converts it to a PHP variable. You can use this method to convert an array into a string. For example,

<?php
$myarray = array(2,4,6);
print json_encode($myarray);

Output

[2,4,6]

Conclusion

As we have discussed above, the Array to string conversion occurs when your PHP code tries to handle an array variable like a string. The best way to avoid this is to alter the logic of your code or check the type of the variable you are trying to convert.    

All the above examples are used for printing the value of an array. But if you want to use the value of the array for some operation, you can use for each function.