PHP: Hypertext Preprocessor (original) (raw)

implode

(PHP 4, PHP 5, PHP 7, PHP 8)

implode — Join array elements with a string

Description

Alternative signature (not supported with named arguments):

Legacy signature (deprecated as of PHP 7.4.0, removed as of PHP 8.0.0):

Parameters

separator

Optional. Defaults to an empty string.

array

The array of strings to implode.

Return Values

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

Changelog

Version Description
8.0.0 Passing the separator after the array is no longer supported.
7.4.0 Passing the separator after the array (i.e. using the legacy signature) has been deprecated.

Examples

Example #1 implode() example

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

Notes

Note: This function is binary-safe.

See Also

Found A Problem?

houston_roadrunner at yahoo dot com

16 years ago

`it should be noted that an array with one or no elements works fine. for example:

"; echo "a2 is: '".implode("','",$a2)."'
"; echo "a3 is: '".implode("','",$a3)."'
"; ?>

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:

'four', 'five', '3rd' => 'six' ); echo implode( ',', a),′/′,implode(′,′,a ),'/', implode( ',', a),/,implode(,,b ); ?>

outputs:
one,two,three/four,five,six

`

omar dot ajoue at kekanto dot com

12 years ago

`Can also be used for building tags or complex lists, like the following:

  • " . implode("
  • ", $elements) . "
  • ";?>

    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:
    <?phpclass Foo
    {
    protected $title;

    public function

    __construct($title)
    { this−>title=this->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:

    Output:
    string(3) "111"

    TRUE became "1", FALSE became nothing.

    `

    Honk der Hase

    5 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.

    array,array, array,symbol = '=') { return implode($glue, array_map( function($k, v)use(v) use(v)use(symbol) { return k.k . 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

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

    returns
    NULL

    This threw me for a little while.

    `

    masterandujar

    12 years ago

    `Even handier if you use the following:

    idnums=array(1,6,12,18,24);id_nums = array(1,6,12,18,24);idnums=array(1,6,12,18,24);id_nums = implode(", ", idnums);id_nums);idnums);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.

    `

    Anonymous

    9 years ago

    `null values are imploded too. You can use array_filter() to sort out null values.

    `