PHP 7.0.6 Released

implode

(PHP 4, PHP 5, PHP 7)

implodeJoin array elements with a string

Description

string implode ( string $glue , array $pieces )
string implode ( array $pieces )

Join array elements with a glue string.

Note:

implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.

Parameters

glue

Defaults to an empty string.

pieces

The array of strings to implode.

Return Values

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

Examples

Example #1 implode() example

<?php

$array 
= array('lastname''email''phone');
$comma_separated implode(","$array);

echo 
$comma_separated// lastname,email,phone

// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""

?>

Notes

Note: This function is binary-safe.

See Also

User Contributed Notes

houston_roadrunner at yahoo dot com
7 years ago
it should be noted that an array with one or no elements works fine. for example:

<?php
    $a1
= array("1","2","3");
   
$a2 = array("a");
   
$a3 = array();
   
    echo
"a1 is: '".implode("','",$a1)."'<br>";
    echo
"a2 is: '".implode("','",$a2)."'<br>";
    echo
"a3 is: '".implode("','",$a3)."'<br>";
?>

will produce:
===========
a1 is: '1','2','3'
a2 is: 'a'
a3 is: ''
omar dot ajoue at kekanto dot com
3 years ago
Can also be used for building tags or complex lists, like the following:

<?php

$elements
= array('a', 'b', 'c');

echo
"<ul><li>" . implode("</li><li>", $elements) . "</li></ul>";

?>

This is just an example, you can create a lot more just finding the right glue! ;)
php.net {at} nr78 {dot} net
11 years ago
Also quite handy in INSERT statements:

<?php

  
// array containing data
  
$array = array(
     
"name" => "John",
     
"surname" => "Doe",
     
"email" => "j.doe@intelligence.gov"
  
);

  
// build query...
  
$sql  = "INSERT INTO table";

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

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

  
// execute query...
  
$result = mysql_query($sql) or die(mysql_error());

?>
Anonymous
3 years ago
It may be worth noting that if you accidentally call implode on a string rather than an array, you do NOT get your string back, you get NULL:
<?php
var_dump
(implode(':', 'xxxxx'));
?>
returns
NULL

This threw me for a little while.
alexey dot klimko at gmail dot com
4 years ago
If you want to implode an array of booleans, you will get a strange result:
<?php
var_dump
(implode('',array(true, true, false, false, true)));
?>

Output:
string(3) "111"

TRUE became "1", FALSE became nothing.
masterandujar
3 years ago
Even handier if you use the following:

<?php
$id_nums
= array(1,6,12,18,24);

$id_nums = implode(", ", $id_nums);
               
$sqlquery = "Select name,email,phone from usertable where user_id IN ($id_nums)";

// $sqlquery becomes "Select name,email,phone from usertable where user_id IN (1,6,12,18,24)"
?>

Be sure to escape/sanitize/use prepared statements if you get the ids from users.
Jacques Amar
2 years ago
Safe way to pass as parameters in IN

<?php
$id_nums
= array(1,6,12,18,24);
$p_types = '';
$qs    = array();
foreach (
$id_nums as $id) {
   
$qs[]   = '?';
   
$p_types .= 'i'; // or whatever type
}
$nums_list = implode(',', $qs);
            
$sqlquery = "Select name,email,phone from usertable where user_id IN ($nums_list)";

$stmt = $dbh->stmt_init();
$stmt->prepare($sqlquery);
// later on, instead of bind:
$parms_array = array_merge(array($p_types), $id_nums);
call_user_func_array(array($stmt,'bind_param'), $parms_array );

// $sqlquery becomes "Select name,email,phone from usertable where user_id IN (?,?,?,?,?)"
?>
Anonymous
9 months ago
null values are imploded too. You can use array_filter() to sort out null values.

<?php
$ar
= array("hello", null, "world");
print(
implode(',', $ar)); // hello,,world
print(implode(',', array_filter($ar, function($v){ return $v !== null; }))); // hello,world
?>
cottton at i-stats dot net
1 year ago
in case you want to implode by keys:
<?php
const POSITION_KEY = 0;
const
POSITION_VAL = 2;
const
POSITION_DESC = 1;

$key = 'key';
$val = 'val';
$desc = 'desc';

$arr = array(
   
POSITION_KEY => $key,
   
POSITION_VAL => $val,
   
POSITION_DESC => $desc,
);
echo
kimplode('=',$arr); // key=desc=val
echo '<br>';
echo
krimplode('=',$arr); // val=desc=key

function kimplode($glue,$arr){
   
ksort($arr);
    return
implode($glue,$arr);
}
function
krimplode($glue,$arr){
   
krsort($arr);
    return
implode($glue,$arr);
}
?>
To Top