php中implode()函数实现字符连接
在php中implode() 函数把数组元素组合为一个字符串了,与explode函数正好是反正,下面来看几个implode函数示例。
语法
implode(separator,array) // array为数组,separator为分切符。
例子
<?php //链接搜索条件 $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 //对提交的数据进行检查 //实例代码: $catidarr = array(); if (!empty($t1)) $catidarr[] = "'" . $t1 . "'"; if (!empty($t2)) $catidarr[] = "'" . $t2 . "'"; if (!empty($t3)) $catidarr[] = "'" . $t3 . "'"; $catidstr = implode(' , ', $catidarr); //用逗号链接所有的操作ID ?>
SQL语句示例:
SELECT uid, name, namestatus FROM ".tname('space')." WHERE uid IN (".simplode($uids).")
例子,批量删除数据
SQL:$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>
好$ID_Dele=$_POST['ID_Dele']将会是一个数组,虽然说PHP是弱类型的,但这里可没ASP弱。ASP可以直接:
SQL="delete from [doing] where id in ('"&ID_Dele&"')"进行删除。但PHP不能把$ID_Dele直接放进去。因为$ID_Dele可不是'1,2,3,4'哦,因为$ID_Dele是一个数组,具有键和值。
好,PHP中也不难,刚好有个函数:implode(),对了。同split()explode()功能刚好相反的一个函数,后两者是用某字符(比如逗号)分割的,而前者则可以拼接为字符串。
因此:
$ID_Dele= implode(",",$_POST['ID_Dele']); $SQL="delete from `doing` where id in ($ID_Dele)";
本文地址:http://www.phprm.com/base/68633.html
转载随意,但请附上文章地址:-)