Home >Backend Development >PHP Tutorial >How to use implode() function to connect characters in php
In phpimplode() function combines the array elements into a string, which is exactly the same as the explode function. Let’s take a look at a few implode function example.
Syntax
implode(separator,array) //array is an array and separator is a separator.
Example
//链接搜索条件 $wheresql = implode(' AND ', $wherearr); //链接搜索条件 function simplode($ids) { return "'".implode("','", $ids)."'"; } $itemidarr = array(); //初始化itemidarr数组 if(empty($_POST['item'])) { //判断提交过来的是否存在待操作的记录,如果没有,则显示提示信息并退出 showmessage('space_no_item'); } $itemidstr = simplode($_POST['item']); //用逗号链接所有的操作ID //对提交的数据进行检查
Instance code:
$catidarr = array(); if(!empty($t1)) $catidarr[] = '\''.$t1.'\''; if(!empty($t2)) $catidarr[] = '\''.$t2.'\''; if(!empty($t3)) $catidarr[] = '\''.$t3.'\''; $catidstr = implode(' , ', $catidarr); //用逗号链接所有的操作ID
SQL statement example:
SELECT uid, name, namestatus FROM ".tname('space')." WHERE uid IN (".simplode($uids).")
Example, batch Delete data
SQL: $SQL="delete from `doing` where id in ('1,2,3,4')";
Data is separated by commas.
Form:e271d5b1456f0c646a1bc2af0d89246c
7bcce7e9c1c0bd7b7e7519a71d524605
fc08c2e7f9bbac5b5f3b20e3b2d728e9
6b01edc9a30b3223f6e5b3784543a46c
8574edf273e16986533b422c6a1b935e
ebe8b7b257a8de063667a9b694b4fc0d
< ;/form>
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 exactly the opposite function to split()explode(). The latter two are separated by a certain character (such as a comma), while the former can be spliced into a string.
The above is the detailed content of How to use implode() function to connect characters in php. For more information, please follow other related articles on the PHP Chinese website!