implode
(PHP 4, PHP 5, PHP 7, PHP 8)
implode — 用字符串连接数组元素
说明
替代写法(不支持命名参数):
遗留写法(从 PHP 7.4.0 起废弃,从 PHP 8.0.0 中移除):
用一个 separator
字符串连接数组元素。
参数
separator
-
可选。默认为空字符串。
array
-
要使用字符串连接的数组。
返回值
返回一个包含所有数组元素并且顺序相同的字符串, 每个元素之间有 separator 分隔。
更新日志
版本 | 说明 |
---|---|
8.0.0 |
在 array 之后传递 separator 已不再支持。
|
7.4.0 |
在 array 之后传递 separator (即:使用遗留写法)已被废弃。
|
示例
示例 #1 implode() 例子
<?php
$array = ['lastname', 'email', 'phone'];
var_dump(implode(",", $array)); // string(20) "lastname,email,phone"
// Empty string when using an empty array:
var_dump(implode('hello', [])); // string(0) ""
// The separator is optional:
var_dump(implode(['a', 'b', 'c'])); // string(3) "abc"
?>
注释
注意: 此函数可安全用于二进制对象。
参见
- explode() - 使用一个字符串分割另一个字符串
- preg_split() - 通过一个正则表达式分隔字符串
- http_build_query() - 生成 URL-encode 之后的请求字符串
+添加备注
用户贡献的备注 9 notes
houston_roadrunner at yahoo dot com ¶
15 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: ''
ASchmidt at Anamera dot net ¶
6 years ago
It's not obvious from the samples, if/how associative arrays are handled. The "implode" function acts on the array "values", disregarding any keys:
<?php
declare(strict_types=1);
$a = array( 'one','two','three' );
$b = array( '1st' => 'four', 'five', '3rd' => 'six' );
echo implode( ',', $a ),'/', implode( ',', $b );
?>
outputs:
one,two,three/four,five,six
omar dot ajoue at kekanto dot com ¶
11 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! ;)
Felix Rauch ¶
8 years ago
It might be worthwhile noting that the array supplied to implode() can contain objects, provided the objects implement the __toString() method.
Example:
<?php
class Foo
{
protected $title;
public function __construct($title)
{
$this->title = $title;
}
public function __toString()
{
return $this->title;
}
}
$array = [
new Foo('foo'),
new Foo('bar'),
new Foo('qux')
];
echo implode('; ', $array);
?>
will output:
foo; bar; qux
alexey dot klimko at gmail dot com ¶
13 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.
Honk der Hase ¶
4 years ago
If you want to implode an array as key-value pairs, this method comes in handy.
The third parameter is the symbol to be used between key and value.
<?php
function mapped_implode($glue, $array, $symbol = '=') {
return implode($glue, array_map(
function($k, $v) use($symbol) {
return $k . $symbol . $v;
},
array_keys($array),
array_values($array)
)
);
}
$arr = [
'x'=> 5,
'y'=> 7,
'z'=> 99,
'hello' => 'World',
7 => 'Foo',
];
echo mapped_implode(', ', $arr, ' is ');
// output: x is 5, y is 7, z is 99, hello is World, 7 is Foo
?>
Anonymous ¶
11 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.
masterandujar ¶
12 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.