Example
Combine array elements into a string:
<?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr); ?>
Definition and usage
implode() function returns a string composed of array elements.
Note: The implode() function accepts two parameter orders. However, due to historical reasons, explode() does not work. You must ensure that the separator parameter comes before the string parameter.
Note: The separator parameter of the implode() function is optional. However, for backward compatibility, it is recommended that you use two parameters.
Note: This function is binary safe.
Syntax
implode(separator,array)
Parameters Description
separator Specifies what is placed between array elements. Default is "" (empty string).
array Required. Arrays to be combined into strings.
Technical details
Return value: Returns a string composed of array elements.
PHP Version: 4+
##Change Log: In PHP 4.3.0 , the separator parameter becomes optional. More examplesExample 1Separate array elements with different characters:<?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr)."<br>"; echo implode("+",$arr)."<br>"; echo implode("-",$arr)."<br>"; echo implode("X",$arr); ?>
$SQL="delete from `doing` where id in ('1,2,3,4')";
<form action="?action=doing" method="post"> <input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="1"/> <input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="2"/> <input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="3"/> <input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="4"/> <input type="submit"/> </form>
SQL="delete from [doing] where id in ('"&ID_Dele&"')". But PHP cannot put $ID_Dele directly into it. Because $ID_Dele is not '1,2,3,4', because $ID_Dele is an array with keys and values.
Okay, it’s not difficult in PHP. There happens to be a function: implode(), that’s right. A function that has the exact opposite function of split()\explode(). The latter two are separated by a certain character (such as a comma), while the former can be spliced into a string.
Therefore:
$ID_Dele= implode(",",$_POST['ID_Dele']);$SQL="delete from `doing` where id in ($ID_Dele)";
The above is the detailed content of PHP returns a string composed of array elements function implode(). For more information, please follow other related articles on the PHP Chinese website!