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. $STRING = implode("SEPARATOR", $ARRAY);
  2. $STRING = array_reduce($ARRAY, "FUNCTION");
  3. Manually loop and create a string.
    • $STRING = "";
    • foreach ($ARRAY as $i) { $STRING .= $i; }
  4. $STRING = json_encode($ARRAY);
  5. $STRING = serialize($ARRAY);

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.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes String To Array Useful Bits & Links
Tutorial Video The End

 

 

DOWNLOAD & NOTES

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

 

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.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer 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.

 

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
$thearray = ["Red", "Green", "Blue", "Shades of Grey"];

// NO SEPARATOR - SIMPLY JOIN ALL ELEMENTS
$thestring = implode($thearray);
echo $thestring; // RedGreenBlueShades of Grey

// SEPARATOR SPECIFIED
$thestring = implode(", ", $thearray);
echo $thestring; // Red, Green, Blue, Shades of Grey

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
function reduction ($carry, $item) {
  if ($item !="Green") {
    $carry .= "$item, ";
  }
  return $carry;
}

$thearray = ["Red", "Green", "Blue", "Shades of Grey"];
$thestring = array_reduce($thearray, "reduction");
$thestring = substr($thestring, 0, -2);
echo $thestring; // Red, Blue, Shades of GreyT

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
$thearray = ["Red", "Green", "Blue", "Shades of Grey"];
$thestring = "";

foreach ($thearray as $item) {
  $thestring .= "$item, ";
}
$thestring = substr($thestring, 0, -2);
echo $thestring; // Red, Green, Blue, Shades of Grey

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

 

4) JSON ENCODE

4-json.php
$thearray = ["Red", "Green", "Blue", "Shades of Grey"];
$thestring = json_encode($thearray);
echo $thestring; // ["Red","Green","Blue","Shades of Grey"]

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
$thearray = ["Red", "Green", "Blue", "Shades of Grey"];
$thestring = serialize($thearray);
echo $thestring; // a:4:{i:0;s:3:"Red";i:1;s:5:"Green";i:2;s:4:"Blue";i:3;s:14:"Shades of Grey";}

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 into the database, and retrieve for later use.

 

USEFUL 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 *