文字

implode

(PHP 4, PHP 5)

implode将一个一维数组的值转化为字符串

说明

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

glue 将一维数组的值连接为一个字符串。

Note:

因为历史原因, implode() 可以接收两种参数顺序,但是 explode() 不行。不过按文档中的顺序可以避免混淆。

参数

glue

默认为空的字符串。

pieces

你想要转换的数组。

返回值

返回一个字符串,其内容为由 glue 分割开的数组的值。

范例

Example #1 implode() 例子

<?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 之后的请求字符串

用户评论:

[#1] lionjsa at ngs dot ru [2015-10-06 05:49:18]

<?php
$a 
= [100"101.45"102.004];

$s implode(', '$a); //excpected "100, 101.45, 102.004", got "100, 101.45, 102,004"
?>

PS: my batch insert failed

[#2] Anonymous [2015-07-20 11:55:18]

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
?>

[#3] cottton at i-stats dot net [2014-05-11 10:57:12]

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);
}
?>

[#4] Jacques Amar [2014-04-11 19:26:17]

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 (?,?,?,?,?)"
?>

[#5] omar dot ajoue at kekanto dot com [2013-03-18 14:21:09]

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! ;)

[#6] Anonymous [2013-02-26 15:56:31]

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.

[#7] masterandujar [2012-09-03 06:15:39]

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.

[#8] alexey dot klimko at gmail dot com [2011-06-23 05:04:36]

If you want to implode an array of booleans, you will get a strange result:
<?php
var_dump
(implode('',array(truetruefalsefalsetrue)));
?>


Output:
string(3) "111"

TRUE became "1", FALSE became nothing.

[#9] houston_roadrunner at yahoo dot com [2009-04-07 11:50:24]

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: ''

[#10] php.net {at} nr78 {dot} net [2005-03-30 06:50:32]

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());

?>

上一篇: 下一篇: