PHP 5.6.0beta1 released

implode

(PHP 4, PHP 5)

implode配列要素を文字列により連結する

説明

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

配列の要素を glue 文字列で連結します。

注意:

implode()は、歴史的な理由により、引数をどちら の順番でも受けつけることが可能です。しかし、 explode() との統一性の観点からは、 ドキュメントに記述された引数の順番を使用する方が混乱が少なくなるでしょう。

パラメータ

glue

デフォルトは空文字列です。

pieces

連結したい文字列の配列。

返り値

すべての配列要素の順序を変えずに、各要素間に glue 文字列をはさんで 1 つの文字列にして返します。

変更履歴

バージョン 説明
4.3.0 glue パラメータがオプションとなりました。

例1 implode() の例

<?php

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

echo 
$comma_separated// lastname,email,phone

// 空の配列を使うと空文字列となります
var_dump(implode('hello', array())); // string(0) ""

?>

注意

注意: この関数はバイナリデータに対応しています。

参考

add a note add a note

User Contributed Notes 7 notes

up
61
houston_roadrunner at yahoo dot com
5 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: ''
up
24
omar dot ajoue at kekanto dot com
1 year 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! ;)
up
22
php.net {at} nr78 {dot} net
9 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());

?>
up
1
Jacques Amar
5 days 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 (?,?,?,?,?)"
?>
up
10
alexey dot klimko at gmail dot com
2 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.
up
5
Anonymous
1 year 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.
up
0
masterandujar
1 year 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.
To Top