Home > Backend Development > PHP Tutorial > How to use implode() function to connect characters in php

How to use implode() function to connect characters in php

伊谢尔伦
Release: 2023-03-11 16:04:02
Original
1786 people have browsed it

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
  //对提交的数据进行检查
Copy after login

Instance code:

$catidarr = array();
if(!empty($t1)) $catidarr[] = '\''.$t1.'\'';
if(!empty($t2)) $catidarr[] = '\''.$t2.'\'';
if(!empty($t3)) $catidarr[] = '\''.$t3.'\'';
$catidstr = implode(' , ', $catidarr); //用逗号链接所有的操作ID
Copy after login

SQL statement example:

SELECT uid, name, namestatus FROM ".tname('space')." WHERE uid IN (".simplode($uids).")
Copy after login

Example, batch Delete data

SQL: $SQL="delete from `doing` where id in ('1,2,3,4')";

Data is separated by commas.

Form:

action="?action=doing" method="post">

< ;/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!

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