IT Learn

Top Menu

  • Home
  • Guest Posting
  • Contact Us

Main Menu

  • PHP
  • MySql
  • IT Learn
  • Home
  • Guest Posting
  • Contact Us

logo

Header Banner

IT Learn

  • PHP
  • MySql
  • IT Learn
PHP
Home›PHP›Convert PHP Arrays to String with Implode() in PHP 8

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

By IT Learn
April 9, 2023
550
0
Share:

Today we are going to learn how we can convert PHP arrays to strings using PHP 8 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";}
?>

Next Article

How to Manage Session in PHP 8 ...

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0
  • 0

IT Learn

General News Blog is fantastic platform for all our readers as we provide them with an ample of valuable information over a plethora of worldwide topics. It is a real honour for us to publish important news and current updates over matters like Business, Travel, Career Advice, Trading, Investment, Visa Rules, Health Tips and much more...

Related articles More from author

  • PHP

    How to merge two array into single array in php?

    April 11, 2023
    By IT Learn
  • PHP

    How to Manage Session in PHP 8 Application

    April 9, 2023
    By IT Learn

Leave a reply Cancel reply

You might be interested

    Timeline

    • March 20, 2025

      576615001742432602

    • February 13, 2025

      328198701739408866

    • April 11, 2023

      How to merge two array into single array in php?

    • April 9, 2023

      How to Manage Session in PHP 8 Application

    • April 9, 2023

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

    ABOUT US

    General News Blog is fantastic platform for all our readers as we provide them with an ample of valuable information over a plethora of worldwide topics. It is a real honour for us to publish important news and current updates over matters like Business, Travel, Career Advice, Trading, Investment, Visa Rules, Health Tips and much more…

    Timeline

    • March 20, 2025

      576615001742432602

    • February 13, 2025

      328198701739408866

    © Copyright IT Learn. All rights reserved.