Home > Backend Development > PHP Tutorial > PHP returns a string composed of array elements function implode()

PHP returns a string composed of array elements function implode()

黄舟
Release: 2023-03-16 22:14:01
Original
1696 people have browsed it

Example

Combine array elements into a string:

<?php
$arr = array(&#39;Hello&#39;,&#39;World!&#39;,&#39;Beautiful&#39;,&#39;Day!&#39;);
echo implode(" ",$arr);
?>
Copy after login

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)
Copy after login

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 examples

Example 1

Separate array elements with different characters:

<?php
$arr = array(&#39;Hello&#39;,&#39;World!&#39;,&#39;Beautiful&#39;,&#39;Day!&#39;);
echo implode(" ",$arr)."<br>";
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>"; 
echo implode("X",$arr);
?>
Copy after login

php implode function application

SQL:

$SQL="delete from `doing` where id in (&#39;1,2,3,4&#39;)";
Copy after login

Data is separated by commas.


Form:

<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>
Copy after login

Okay $ID_Dele=$_POST['ID_Dele'] will be an array. Although PHP is weakly typed, it is not as weak as ASP. ASP can delete directly:

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[&#39;ID_Dele&#39;]);$SQL="delete from `doing` where id in ($ID_Dele)";
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template