PHP 7.1.12 Released

implode

(PHP 4, PHP 5, PHP 7)

implodeRassemble les éléments d'un tableau en une chaîne

Description

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

Rassemble les éléments d'un tableau en une chaîne.

Note:

implode() peut, pour des raisons historiques, accepter les paramètres dans un sens ou dans l'autre. Pour des raisons de cohérence avec explode(), toutefois, il est préférable d'utiliser l'ordre des arguments tels que documenté.

Liste de paramètres

glue

Par défaut, une chaîne vide.

pieces

Le tableau de chaînes à rassembler.

Valeurs de retour

Retourne une chaîne contenant la représentation en chaîne de caractères de tous les éléments du tableau pieces, dans le même ordre, avec la chaîne glue, placée entre deux éléments.

Exemples

Exemple #1 Exemple avec implode()

<?php

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

echo 
$comma_separated// lastname,email,phone

// Chaîne vide lors de l'emploi d'un tableau vide :
var_dump(implode('hello', array())); // string(0) ""

?>

Notes

Note: Cette fonction gère les chaînes binaires.

Voir aussi

add a note add a note

User Contributed Notes 16 notes

up
214
houston_roadrunner at yahoo dot com
8 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
85
omar dot ajoue at kekanto dot com
4 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
65
php.net {at} nr78 {dot} net
12 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
31
alexey dot klimko at gmail dot com
6 years ago
If you want to implode an array of booleans, you will get a strange result:
<?php
var_dump
(implode("'span>);irue;/span>, $rue;/span>, $fals;/span>, $fals;/span>, $rue;/span>,));
?>

Tutput<

up
down
212
aAnonymous/em> If manybe uordth"oteng thet gi you waccidntatly" ctlyimplode an an tring(ratier Shetnan array you wdo NOTget ayur dtring(rback you wet aNULL
<?php
var_dump
(implode("':, 'cxxxxx);;
?>

title="584 like this..."> 212
tlass="name"> amater andujar/em> para;
div class="phpcode"> IEve handlir Si you wserthe following:< br />< br /><?php< br /><$id_num = array(&)$6/span>)$1)$18/span>)$24/span>);< br />< br />$sid_num = a/span>implode("',"; $aid_num /span>);< br />$ssqluery<= "ISlect oame,email,phone< fromwserrable" wher usedr_id IN $sid_num /span>"";< br />< br />// essqluery<$>< br />< br />
up
down
23
aradoslawdot cpaluszakat gmail dot com61yearsago
Al vry<<?php
<$picamesp
= array("apic1.jpg; "apic2.jpg; "apic3.jpg; "apic4.jpg; "apic5.jpg; "apic6.jpg; "apic7.jpg;;
?$llypic = a/span>implode("'|, array_kslice/apan>(i$picamesp/span>, a0/span>, a5/span>);;&/span>?>

THop fithelp
aFelix Rauh
Ift mght ge uordthwhle."oteng thet ghe frray ouppolid th implode ()can crntaini ojects<,prodide&dghe fojects
<?php

&/span>)lass=aFoobr />&/span>){br />$aitle=/span>;

 &  &rublic unction.
$__onstauctu/apan>(i$itle=/span>;)br /> &  &{br />$ahis./span>;-gt; span class="default">$rtle== a/span>iaitle=/span>;

 &  &rublic unction.
$__toSring(/span>())br /> &  &{br />$ahis./span>;-gt; span class="default">$rtle=/span>;
<}br />
&/span>$array = a[br />aFoobspan>("'foo);
    &new
aFoobspan>("'bar);
    &new
aFoobspan>("'qux);br />&]

echo
implode("'; , $array).
?>

will potput<

up
title="582 like this..."> 23
tlass="name"> aAnonymous/em> para;
div class="phpcode"> Inul palues oarermplode&ditoo. Yu can csertrray_kfilter0)mo sert aotp nul palues .br />
<?php
<$rsa/span>= array("aello', anul /span>; "aordld;;
implode("',, $arr/span>);;<// eello',,ordldbr />&/span>)pingtimplode("',, $rray_kfilter/apan>(i$rr/span>), unction.i$v/span>);{$ava/span>=!= a/span>inul /span>; <});;<// eello',ordldbr />&/span>?>
up
down
21
aveyselkorkmaz44at gmail dot com419 day ago
Ibpan class="default"><?php

&$dizia/span>= array("'soyadı, 'cepost', 'btelefon);
$avirgül_ayraçlı= a/span>implode("',, $array).

echo
iavirgül_ayraçlı/span>; // esoyadı,epost',telefonbr />
?>

TBU İŞLEM YANLIŞ!!!!br />
&$rray &br /><?php

$edizia/span>= array("'soyadı, 'cepost', 'btelefon);
$avirgül_ayraçlı= a/span>implode("',, $adizi/span>).

echo
iavirgül_ayraçlı/span>; // esoyadı,epost',telefonbr />
?>
title="580 like this..."> 20
tlass="name"> aadminat glanlinkdot cet {ot aju/em> para;
62 month ago
div class="phpcode"> Ift is possile" or ba array oo hoav fnumrincpalues , as wllo as tring(ralues . Iplode aill pconvet a l pnumrincprray olements
<?php
<$es-u/apan>(=/span>implode(<[/span>"'on",i)$)"3four,i5.67/span>,].
iaes-u/apan>(
// otput?>
up
down
20
amulahalilovncpr yahoo dot com Ibpan class="default"><?php< br />
// Basncploop ito dlt;li>",manyb" or bsomehelp

&/span>$asome_sring(/span>( "'jrd"n cpippen rodanu jeckson iv ac shaq)

?$tring"_to_rray) a/span>ixplode(/span>("' , /span>$asome_sring(/span>(.

eor each
$string"_to_rray<=a=a$alues/span>(.:br />    &br />"<ui>",

 &          a$alues/span>(

 &      &cho
"<u/i>",


&
a>

T/*jrd"n br /><*//span>
title="V49 like this..."> 2-1
tlass="name"> aJacues, Amar/em> para;
div class="phpcode"> ISaf aiy oo hpss= as arameters"in INSbr />
<?php
<$id_num
= array(&)$6/span>)$1)$18/span>,i<4/span>);
?$p_ype" = a/span>"'span>);<
?$qsnbsp;   = array(<;
$sid_num =a=a$id/span>);&{br />?$qs/span>)[]nbsp;  < a/span>"'?span>);<
?$p_ype" == "'ispan>);<<// er bwhte"vr Shpe"br />&/span>)}br />?$num _ists= a/span>implode("',, $aqs/span>);
    &/span>?ssqluery<= "ISlect oame,email,phone< fromwserrable" wher usedr_id IN $snum _ists/span>"";

&/span>$strms= a/span>i$dbh/span>=-gt; span class="default">$trms_iniu/apan>(<;
?$trms/span>=-gt; span class="default">$prparae/span>(?ssqluery);
// eate r on,in steadof boind
?$parm _rray = a/span>?rray_kmerge/span>(&$p_ype" /span>); $aid_num /span>);
?ctly_sedr_unct_rray)&$trms/span>="'oind_arame);<&/span>?$parm _rray =;

// essqluery<?>
up
down
2-1
aerkn ckramta/em> IDiziaiçerisindeki vryiyiaistenilen yapıştırıcı le."birleştirir
&br /><?php
=cho implode("' , <[/span>"'lr ee)"'dolr )"'sit)"'me"d)];<// lr ee dolr sit me"dbr />?>
2-12
athe dot cvn cot ceijndhove hat cello'dot cel/em>
17ymonth ago
Cmplode astopsup!on enount"eing(rNULLbr />
<$myAray <= ('ne<, '"two, '"tref<, 'nul '"four, '"five'.

up
down
2-92
acottto hat i-tatesdot cet /em> Cm claseyou want to implode abykeys
<?php
)onstaiPOSITION_KEY= a/span>?0/span>;
iPOSITION_VAL= a/span>?)
iPOSITION_DESC= a/span>?)

&/span>$seyw= a/span>"'eywspan>);<
?$va . a/span>"'va span>);<
?$desc. a/span>"'desc)

?$arsa/span>= array(?POSITION_KEY= gt; $seyw/span>)?POSITION_VAL= gt; $sva /span>)?POSITION_DESC= gt; $sdesc/span>)<.
ikmplode("'=, /span>$arra/span>=;<// eeyw=desc=va /r />=cho "'lt;?brgt; )
ikrmplode("'=, /span>$arra/span>=;<// eva =desc=eyw/r />
=unction.$kmplode($agues/span>( /span>$arra/span>=;{br />?kert /span>($arra/span>=;
$mplode($agues/span>( /span>$arra/span>=;;br /><}br />$krmplode($agues/span>( /span>$arra/span>=;{br />?krert /span>($arra/span>=;
$mplode($agues/span>( /span>$arra/span>=;;br /><}br />?>
/div>
?a href="/manual/vaddnote.php?isectfunction.implode&vredirectfttp_//pfr2.hp.net/ianual/vfr/unction.implode&.hp.>?aimg src='/imge=s/otes-vadd@2x.png' alt='add a"otee' width='12' heght ='12'>add a"otee/spmlly>/a> /spetion.>