PHP

Converting Arrays to Strings

$address = implode('<br />', $data);

The way back (that is, making a string representation out of the elements in an array) can be done using implode(). Again, two parameters are required: the separation elements, then the array. The order is quite unusual, yet important.

So, PHP joins the elements of the array, using the <br /> HTML element. Therefore, in the browser, each array's elements stay at its own line.

implode function syntax

string implode ( string $glue , array $pieces )

This function accepts two parameters:

  1. glue: any string value to join with i.e dot (.), comma (;), html tag (<br />) or any character.
  2. array: The array of strings to implode.

Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.

Turning an Array into a String

<?php
  $data = array(
    'PHP',
    'MySQL',
    'Ajax',
    'JavaScript',
    'CSS3'
  );
  $data_string = implode('<br />', $data);
  echo $data_string;
?>

by BrainBellupdated
Advertisement: