implode()
(PHP 4, PHP 5, PHP 7)
将一个一维数组的值转化为字符串
说明
implode(string $glue, array $pieces) : string
implode( array $pieces) : string
用$glue将一维数组的值连接为一个字符串。
Note:因为历史原因,implode()可以接收两种参数顺序,但是explode()不行。不过按文档中的顺序可以避免混淆。
参数
- $glue
默认为空的字符串。
- $pieces
你想要转换的数组。
返回值
返回一个字符串,其内容为由 glue 分割开的数组的值。
范例
Example #1implode()例子
<?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) "" ?>
注释
Note:此函数可安全用于二进制对象。
参见
explode()
使用一个字符串分割另一个字符串preg_split()
通过一个正则表达式分隔字符串http_build_query()
生成 URL-encode 之后的请求字符串
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: ''
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! ;)
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
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
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()); ?>
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.
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.
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.
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 ?>
It is possible for an array to have numeric values, as well as string values. Implode will convert all numeric array elements to strings. <?php $test=implode(["one",2,3,"four",5.67]); echo $test; //outputs: "one23four5.67" ?>