implode

(PHP 4, PHP 5, PHP 7)

implodeVerbindet Array-Elemente zu einem String

Beschreibung

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

Verbindet die Elemente eines Array mittels glue zu einer Zeichenkette.

Hinweis:

implode() akzeptiert die Parameter aus historischen Gründen in beiden Reihenfolgen. Aufgrund der Konsistenz zu explode(), ist die nicht dokumentierte Reihenfolge allerdings missbilligt.

Parameter-Liste

glue

Standardmäßig eine leere Zeichenkette.

pieces

Das Array mit den zusammenzufügenden Zeichenketten.

Rückgabewerte

Gibt einen String mit einer String-Repräsentation aller Array-Elemente in der gleichen Reihenfolge wie in dem Array zurück, bei dem die einzelnen Array-Elemente mit dem im Parameter glue angegebenen String verbunden werden.

Changelog

Version Beschreibung
7.4.0 Das Übergeben des glue nach den pieces (d.h. nicht die dokumentierte Reihenfolge der Parameter zu verwenden) wurde missbilligt.

Beispiele

Beispiel #1 implode()-Beispiel

<?php

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

echo 
$comma_separated// lastname,email,phone

// Leere Zeichenkette wenn leeres Array genutzt wird
var_dump(implode('hello', array())); // string(0) ""

?>

Anmerkungen

Hinweis: Diese Funktion ist binary safe.

Siehe auch

  • explode() - Teilt eine Zeichenkette anhand einer Zeichenkette
  • preg_split() - Zerlegt eine Zeichenkette anhand eines regulären Ausdrucks
  • http_build_query() - Erstellen eines URL-kodierten Query-Strings

add a note add a note

User Contributed Notes 10 notes

up
268
houston_roadrunner at yahoo dot com
11 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
9
ASchmidt at Anamera dot net
2 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
up
103
omar dot ajoue at kekanto dot com
7 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! ;)
up
16
Felix Rauch
4 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
up
68
php.net {at} nr78 {dot} net
15 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
33
alexey dot klimko at gmail dot com
9 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
26
Anonymous
7 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.
up
16
masterandujar
8 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.
up
9
Anonymous
5 years ago
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
?>
up
1
admin at lanlink dot net dot au
3 years ago
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"
?>
To Top