字符串函数
在线手册:中文  英文

implode

(PHP 4, PHP 5)

implodeJoin array elements with a string

说明

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.

参数

glue

Defaults to an empty string.

pieces

The array of strings to implode.

返回值

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

更新日志

版本 说明
4.3.0 The glue parameter became optional.

范例

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) ""

?>

注释

Note: 此函数可安全用于二进制对象。

参见


字符串函数
在线手册:中文  英文

用户评论:

Anonymous (2013-04-22 09:32:09)

/*Defining the associative array to keep the name of
              toppings checked*/    
            $arrayOfMeatToppingsSelected = array();
            $arrayOfVegetablesToppingsSelected = array();

            //Imploding of the names of the toppings checked
            $implodedArrayOfMeatToppingsSelected = implode(', ', $arrayOfMeatToppingsSelected);    
            $implodedArrayOfVegetablesToppingsSelected = implode(', ', $arrayOfVegetablesToppingsSelected);            

        <form action="orderConfirmation.php" method="post">
                    <input type="hidden" name="firstName" value="<?php echo $firstName?>" />
                    <input type="hidden" name="lastName" value="<?php echo $lastName?>" />
                    <input type="hidden" name="address" value="<?php echo $address?>" />
                    <input type="hidden" name="city" value="<?php echo $city?>" />
                    <input type="hidden" name="province" value="<?php echo $province?>" />
                    <input type="hidden" name="postalCode" value="<?php echo $postalCode?>" />
                    <input type="hidden" name="telephone" value="<?php echo $telephone?>" />
                    <input type="hidden" name="email" value="<?php echo $email?>" />
                    <input type="hidden" name="numberPizza" value="<?php echo $numberPizza?>" />
                    <input type="hidden" name="sizePizza" value="<?php echo $sizePizza?>" />
                    <input type="hidden" name="crustTypePizza" value="<?php echo $crustTypePizza?>" />
                    <input type="hidden" name="toppings" value="<?php echo $checkBoxCount?>" />
                    <input type="hidden" name="extrasPizza" value="<?php echo $extrasPrice?>" />
                    <input type="hidden" name="price" value="<?php echo $price?>" />
                    <input type="hidden" name="priceTax" value="<?php echo $priceTax?>" />
                    <input type="hidden" name="finalPrice" value="<?php echo $finalPrice?>" />
                    <input type="hidden" name="deletion" value="<?php echo "1"?>" />
                    <input type="hidden" name="implodedArrayOfMeatToppingsSelected" 
                        value="<?php echo $implodedArrayOfMeatToppingsSelected?>" />
                    <input type="hidden" name="implodedArrayOfVegetablesToppingsSelected" 
                        value="<?php echo $implodedArrayOfVegetablesToppingsSelected?>" />
            
                    <input type="submit" value="Submit" />
                </form>

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

jento (2013-03-06 01:21:27)

function implode_r($glue,$arr){
$ret_str = "";
foreach($arr as $a){
$ret_str .= (is_array($a)) ? implode_r($glue,$a) : "," . $a;
}
return $ret_str;
}

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.

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

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.

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

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

?>

Cedric at isoca dot com (2002-07-11 10:39:14)

Implode with an unset array will made a warning and fail, but is ok with an empty array.
So if you don't trust the content of the array, allways initialize it before :
$param = array();
[...]
echo implode('&', $param);

易百教程