5 Ways To Convert Array To String In PHP

Welcome to a beginner’s tutorial on how to convert an array to a string in PHP. So you need to quickly create a string from data in an array?

There are a couple of methods that we can use to convert an array to a string in PHP:

  1. $STR = implode("SEPARATOR", $ARR);
  2. $STR = array_reduce($ARR, "FUNCTION");
  3. Manually loop and create a string.
    • $STR = "";
    • foreach ($ARR as $i) { $STR .= $i; }
  4. $STR = json_encode($ARR);
  5. $STR = serialize($ARR);

That covers the basics, but just how does each one of them work? Need more actual examples? Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

 

DOWNLOAD & NOTES

First, here is the download link to the example source code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

ARRAY TO STRING

All right, let us now get started with the various ways to convert a string to an array in PHP.

 

1) IMPLODE FUNCTION

1-implode.php
// (A) THE ARRAY
$arr = ["Red", "Green", "Blue"];

// (B) NO SEPARATOR - SIMPLY JOIN ALL ELEMENTS
$str = implode($arr);
echo $str; // RedGreenBlue

// (C) SEPARATOR SPECIFIED
$str = implode(", ", $arr);
echo $str; // Red, Green, Blue

This should be pretty easy to understand. The implode() function simply takes an array and combines all the elements into a flat string. Just remember to specify a SEPARATOR, or all the elements will be smushed into a string without any spaces.

 

 

2) ARRAY REDUCE

2-reduce.php
// (A) REDUCTION FUNCTION
function reduction ($carry, $item) {
  if ($item !="Green") { $carry .= "$item, "; }
  return $carry;
}
 
// (B) THE ARRAY
$arr = ["Red", "Green", "Blue"];
 
// (C) REDUCE ARRAY
$str = array_reduce($arr, "reduction");
$str = substr($str, 0, -2);
echo $str; // Red, Blue

The usage of the array_reduce() function may not obvious at the first glance, but the general idea is to reduce an array down to a single string – By using a given custom REDUCTION function – In this one, we can pretty much set any rules, do any “special processing”.

 

3) MANUAL LOOP

3-loop.php
// (A) THE ARRAY
$arr = ["Red", "Green", "Blue"];
 
// (B) MANUAL LOOP & BUILD STRING
$str = "";
foreach ($arr as $item) { $str .= "$item, "; }
$str = substr($str, 0, -2);
echo $str; // Red, Green, Blue

Well, this should be pretty self-explanatory. Just loop through an array, and create a string manually.

 

4) JSON ENCODE

4-json.php
// (A) THE ARRAY
$arr = ["Red", "Green", "Blue"];
 
// (B) JSON ENCODE ARRAY
$str = json_encode($arr);
echo $str; // ["Red","Green","Blue"]

For the beginners, JSON (Javascript Object Notation) in simple terms, is to convert an array into an equivalent data string – Very handy if you want to pass an array from Javascript to PHP (or vice-versa). In PHP, we use json_encode() to turn an array into a string, and json_decode() to turn the string back to an array.

 

 

5) SERIALIZE

5-serialize.php
// (A) THE ARRAY
$arr = ["Red", "Green", "Blue"];
 
// (B) PHP SERIALIZED ARRAY
$str = serialize($arr);
echo $str; // a:3:{i:0;s:3:"Red";i:1;s:5:"Green";i:2;s:4:"Blue";}

Finally, this is PHP’s “version of JSON encode”. Yep, the serialize() function can pretty much turn any array, object, function, and whatever into a string. Very convenient if you want to store something in the database, and retrieve it for later use.

 

EXTRA BITS & LINKS

That’s all for this guide, and here is a small section on some extras and links that may be useful to you.

 

SUMMARY

Function Description Reference Link
implode(SEPARATOR, ARRAY) “Glues” an array together, elements separated with the defined SEPARATOR. Click Here
array_reduce(ARRAY, FUNCTION) Reduces the specified ARRAY down into a single string, using the rules provided in the custom FUNCTION. Click Here
json_encode(ARRAY) Encodes the given array into a JSON string. Click Here
serialize(ARRAY) Serializes a given array, object, function into a string. Click Here

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

Convert Array To String In PHP (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *