How to fix array to string conversion in PHP

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Array to string conversion in PHP

Array to string conversion in PHP warning occurs when PHP tries to implicitly convert array to string. One of the recommended fixes is as follows.

<?php
 
$array = [
    "a" => ["b" => ["c" => "d"]],
    "e" => "f",
    "g" => ["h", "i"=>["j","k"]]
];
 
echo 'The array is: '.json_encode($array); //The array is: {"a":{"b":{"c":"d"}},"e":"f","g":{"0":"h","i":["j","k"]}}
 
?>

There are other fixes too. Stay tuned to the article to learn more.

Introduction

PHP implicitly converts primitive types like integer, string, and boolean to other types based on the context. For instance, the following example demonstrates implicit conversion. Learn more about type conversions.

<?php
$integer = 101;
$boolean = true;
$string = '2';
 
echo 'Hello to PHP '.$integer.'!!'; //Hello to PHP 101!!
echo  2 + $boolean; //3
echo  3 * $string; //6
 
?>

See how PHP infers the type from the context. It converts boolean (true) to 1 and adds it to integer 2. Similarly, it casts string ‘2’ to integer 2 and multiplies it with integer 3.

However, this isn’t the case when implicitly converting an array to other types. One popular example is converting an array to a string. The following example demonstrates h

<?php
 
$array = [1, 2, 3, 4, 5];
 
echo 'The array is: '.$array; //PHP Warning:  Array to string conversion
 
?>

So without any further ado, let’s learn how to go about this notice/warning.

Fix#1 – Array to string conversion in PHP using implode

PHP implode function joins array elements with a string based on the delimiter argument. It explicitly converts an array to a string. Here’s how it works.

<?php
 
$array = [1, 2, 3, 4, 5];
 
echo 'The array is: '.implode(',', $array); //The array is: 1,2,3,4,5
 
?>

PHP implode works great for one-dimensional arrays. However, this fix may not work as intended for complex multidimensional arrays. So, let’s move on to a more robust fix.

Fix#2 – Array to string conversion in PHP using json_encode

PHP json_encode returns a JSON representation of an array. The JSON representation is of the string type. Besides, JSON is a well-acclaimed data exchange format, and thereby it is highly recommended to use json_encode for arrays.

<?php
 
$array = [1, 2, 3, 4, 5];
 
echo 'The array is: '.json_encode($array); //The array is: [1,2,3,4,5]
 
?>

This method works well for complex arrays. It converts it into string while preserving the original structure. The following example demonstrates this.

<?php
 
$array = [
    "a" => ["b" => ["c" => "d"]],
    "e" => "f",
    "g" => ["h", "i"=>["j","k"]]
];
 
echo 'The array is: '.json_encode($array); //The array is: {"a":{"b":{"c":"d"}},"e":"f","g":{"0":"h","i":["j","k"]}}
 
?>

Fix#3 – Array to string conversion in PHP using foreach loop

Finally, using a PHP loop gives you more control over the logic as you traverse the array elements. A foreach loop is good enough for a one-dimensional array. For multidimensional, recursive algorithms may be more favorable.

<?php
 
$array = [1, 2, 3, 4, 5];
 
echo 'The array is: [';
 
foreach($array as $k=>$v)
{
    echo $v.', ';
}
 
echo ']';
 
//OUTPUT
//The array is: [1, 2, 3, 4, 5, ]
?>

Voila! That’s all for this article. Let’s wrap it up!

Wrap Up

This article explains how to fix array to string conversion in PHP. One of the recommended fixes is using json_encode which is robust enough to convert any sort of array to a string. Unlike primitive data types, PHP doesn’t coerce array implicitly.

Hopefully, you’ve enjoyed the article. Stay tuned for more articles like these at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.