Convert PHP Arrays to String Using Implode() Function in PHP 7

Article By Rawat Digamber on
Today we are going to learn how we can convert PHP arrays to strings using PHP 7’s implode() function. This function makes array to string conversion in php very easy.

Understand PHP’s implode() function

PHP’s implode() function takes array as a value and returns a string.

Implode Function’s Syntax

Implode function takes 2 parameters seprator and $array let’s find out how it works?

implode(separator,array)
ParameterDetail
SeparatorIt’s an optional parameter. This parameter allows you to insert seprator type in your array element. It comes with ” “ empty string.
ArrayThis is an array which will be converted to a string. It’s an enviable parameter.

Convert PHP Arrays to Strings Examples

Let’s find out how array to string conversion works in PHP, In the below examples i am going to take an array and convert it to the string using PHP’s implode() function.

<?php
  $array = ['Waiting', 'for', 'Avenger', 'End', 'Game'];
  echo implode(" ",$array); // Result: Waiting for Avenger End Game
?>

In below example, we are going to skip the separator parameter. You will se that implode function will add an empty string between array’s elements.

<?php
  $array = ['A', 'v', 'e', 'n', 'g', 'e', 'r'];
  echo implode($array); // Avenger 
?>

Let’s check out if your consist non-string values, in this condition PHP’s implode function will convert all your non-string values to string values.
Note: Implode function convertes true to 1 and NULL & empty value to empty string.

<?php
  $array = [1, false, 0, true, NULL, 0.07];
  echo implode(', ', $array); // 1, , 0, 1, , 0.07 
?>

Conversion result for the some non-string values by implode function:

  • true = 1
  • NULL = empty string
  • empty = empty string

Convert Array of Arrays to String Using PHP’s Implode Function

If you have multiple arrays within your arrays in PHP it will return arrays as a result. Check out in the example below.

<?php
  $movies = [
    'comedy' => ['In the Loop', '21 Jump Street', 'Elf ', 'This Is the End'],
    'war' => ['Dunkirk', 'Hacksaw Ridge', 'Inglourious Basterds', 'Defiance'],
    'action' => ['La La Land', 'The Notebook', 'About Time', 'Twilight'],
    'sci fi' => ['Interstellar', 'Annihilation', 'Gravity', 'Inception'],
  ];
  echo implode(', ', $movies); // Array, Array, Array, Array 
?>

Now lets convert multilevel array into string in PHP using custom function below with Implode() function.

<?php
  
  // $movies array
  $movies = [
    'comedy' => ['In the Loop', '21 Jump Street', 'Elf ', 'This Is the End'],
    'war' => ['Dunkirk', 'Hacksaw Ridge', 'Inglourious Basterds', 'Defiance'],
    'action' => ['La La Land', 'The Notebook', 'About Time', 'Twilight'],
    'sci fi' => ['Interstellar', 'Annihilation', 'Gravity', 'Inception'],
  ];

  // function to convert arrays to string
  function convertArraysToString($array, $separator  = ', ') {
          $str = '';
          foreach ($array as $Array) {
                  $string .= implode($separator, $Array);
          }
          return $string;
  }
  
  // pass $movies array and call the function
  echo convertArraysToString($movies);

 // Result => In the Loop, 21 Jump Street, Elf , This Is the EndDunkirk, Hacksaw Ridge, Inglourious Basterds, DefianceLa La Land, The Notebook, About Time, TwilightInterstellar, Annihilation, Gravity, Inception

?>

PHP Convert to Arrays to Strings Using json_encode() and serialize() methods

In this part of this tutorial, we are going to explore more techniques for converting PHP arrays into strings. We are going to work with json_encode() and serialize() methods.

Using PHP JSON encode Method

PHP offers json_encode() function to handle JSON, it converts objects, arrays into JSON.

<?php
   $array = ["Tony Stark", "Steve Rogers", "Bruce Banner", "Thor"];
   $myJson = json_encode($array);
   echo $myJson; // ["Tony Stark","Steve Rogers","Bruce Banner","Thor"]
?>

PHP serialize() Method Example

The serialize() function in PHP produces a storable representation of a value

<?php
   $array = ["Tony Stark", "Steve Rogers", "Bruce Banner", "Thor"];
   $myJson = serialize($array);
   echo $myJson; 
   //Result => a:4:{i:0;s:10:"Tony Stark";i:1;s:12:"Steve Rogers";i:2;s:12:"Bruce Banner";i:3;s:4:"Thor";}
?>

Feel free to contact me, If you are looking for a freelance web and mobile app developer in India, with the following skills: Angular JS, Angular 2+, Node JS, Firebase, MongoDB, Ionic Framework and WordPress.

I also offer remote contracting services to clients across the globe.

Get Started