PHP implode Function | Join Array Elements

PHP implode Function is an inbuilt function of PHP. It joins elements of an array into a single string. It returns a string containing elements of the array. In this article, we discuss the PHP implode Function. Also, we will discuss a few examples demonstrating the usage of this function.

Note: This function is an alias of PHP join Function.

Also, you can learn about PHP explode Function that works just the opposite of implode function.

Syntax

implode( $delimiter ,  $array );

Parameters

PHP implode Function expects two parameters. One parameter is mandatory while the other is optional. However, it accepts the parameters in either order.

  • $delimiter: Delimiter or the separator is an optional parameter and specifies by what character the array elements will be joined. The default value of delimiter is an empty string.
  • $array: $array is an array of strings to implode. It is a mandatory parameter.

Return Value

PHP implode Function returns a string from the elements of the array separated by the given delimiter.

Input: implode('-', array('How', 'Are', 'You?');
Output: How-Are-You?

Input: implode(',', array('apple', 'mango', 'banana');
Output: apple,mango,banana
PHP implode Function Diagram Concatly

Examples

Let us go through some of the examples to demonstrate the use of PHP implode.

Example 1: Imploding with Newline Character

For example, let us implode our array of strings with a ‘\n’ (newline character).

<?php
$array = array('Hello!','What','a','beautiful','day','to','code.!');
$implode = implode("\n",$array);

echo "<pre>";
print_r($implode);
echo "</pre>";
?>
Output

Hello!
What
a
beautiful
day
to
code.!

We can see from the above example that our string got imploded by a newline character.

Example 2: No Value Passed To Function

For instance, let us see in this example that what happens if we do not pass any delimiter to the function.

<?php

$array = array('Hello!','What','a','beautiful','day','to','code.!');
$implode = implode(" ",$array);

echo "<pre>";
print_r($implode);
echo "</pre>";

?>
Hello! What a beautiful day to code.!

By default implode function implodes the array by ‘ ‘ (space), if we do not pass any delimiter to it.

Example 3: Passing Empty Array

If we pass an empty array to implode function it will return an empty string. For example, the following example demonstrates it.

<?php
$array = array();
$implode = implode(" ",$array);

echo "<pre>";
var_dump($implode);
echo "</pre>";

?>
string(0) ""

Example 4: Associative Array

In the above examples, we imploded integer indexed arrays. In this example, we implode associative arrays.

<?php
$array = array('Hi','This','Is','type'=>'Multidimensional','Array');
$implode = implode(" ",$array);

echo "<pre>";
print_r($implode);
echo "</pre>";
Hi This Is Multidimensional Array.

Clearly, from the above output, we can see that implode function only implodes the values of the array and not its keys.

Example 5: Practical Example

We can also use the implode function to make an INSERT statement of MySQL. It is a pretty easy way to fill values into the VALUES part of insert statement if you have an array containing desired values. Below is an example showing the use of PHP implode for creating an INSERT statement.

<?php

   $array = array(
      "name" => "Joe",
      "surname" => "Tribbiani",
      "occupation" => "Actor"
   );

   $sql  = "INSERT INTO table";

   $sql .= " (`".implode("`, `", array_keys($array))."`)";

   $sql .= " VALUES ('".implode("', '", $array)."') ";

echo "<pre>";
print_r($sql);
echo "</pre>";
INSERT INTO table (`name`, `surname`, `occupation`) VALUES ('Joe', 'Tribbiani', 'Actor') 

Conclusion

In conclusion, we discussed PHP implode Function. We also saw some of the examples showing it’s usage. Also, you can also check PHP’s official documentation for the implode function. Moreover, you can read about PHP explode Function that works just the opposite of implode function.

You can learn more about PHP Array Functions on Concatly!

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *