Array implode and explode functions in PHP

This tutorial shows how to use the implode and explode functions on arrays in PHP. The implode function is used to turn an array into a string, whereas the explode function is used to turn a string into an array. Watch the video below and then scroll down to see the commented sample code.

Sample PHP code:

<?php
  $avengers = array("Iron Man","Thor","Hulk","Hawkeye","Black Widow");
  // implode - turns an array into a string
  $avengersString = implode(' ',$avengers);
  // implode(' ',$avengers); will add a space between each array item in the new string

  echo $avengersString; // displays the string

  // explode - turns a string into an array
  $superheroesString = "wolverine,batman,hulk,spider-man,storm,iron man";
  $superheroesArray = explode(',',$superheroesString);
  // explode(',',$superheroesString); will add each item occuring after a comma in
  // the string as a separate element in the array.

  echo $superheroesArray[5]; //displays the 6th element in the array
?>