How to Convert PHP Arrays to String with Implode() in PHP 8

Today we’ll look at how to use PHP 8’s implode() function to convert PHP arrays to strings. This function simplifies array to string conversion in PHP.

Understand the implode() function in PHP.

The implode() function in PHP takes an array as a parameter and returns a string.

Syntax of the Implode Function

Let’s look at how the Implode function works. It has two parameters: separator and $array.

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.

Examples of Converting PHP Arrays to Strings

Let’s look at how array to string conversion works in PHP. In the examples below, I’ll take an array and use PHP’s implode() function to convert it to a string.

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

In the following example, we will skip the separator parameter. You’ll notice that the implode function inserts an empty string between the array’s elements.

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

Let’s see if your contains non-string values; if so, PHP’s implode function will convert all non-string values to string values.

It should be noted that the Implode function converts true to 1 and NULL and empty values to empty strings.

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

Implode function conversion results for some non-string values:

  • 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, the result will be arrays. Consider the following example.

<?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, let’s use the Implode() function to convert a multilevel array to a string in PHP.

<?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 section of the tutorial, we’ll look at more methods for converting PHP arrays to strings. We will use the json encode() and serialise() methods.

Using PHP JSON encode Method

To handle JSON, PHP provides the json encode() function, which converts objects and 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

PHP’s serialise() function generates 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";}
?>

Thank you for taking the time to read this article. I hope it was useful to you. Share your thoughts on the article in the comments section.

1 Comment

Leave a Reply