php字符串函数implode

这片博文是关于php常用函数implode。

函数原型:

 

<?php
implode ($glue, array $pieces);
?>

 参数说明:

 

$glue //这个单词是胶水的意思,实际上是用来连接数组元素的

$pieces //数组

 

函数功能:

把数组元素串行化,每两个元素间用$glue连接,这个函数的功能类似于JS的join函数。

 

PHP手册是这样描述的:Join array elements with a string, returns a string containing a string representation of all the array elements, with the glue string between each element。

看个具体例子

 

<?php
	$arr = array('a','bc','def');
	print_r(implode('<>', $arr));
?>

输出 a<>bc<>def

 

那么,如果数组是个嵌套数组,又会怎样呢?咱再来个例子

<?php
	$arr = array('a',array('bc','def'));
	print_r(implode('<>', $arr));
?>

 也会输出 a<>bc<>def吗??不会!!输出如下内容

 

( ! ) Notice: Array to string conversion in G:\www\BBK\Index.php on line 66
Call Stack
#TimeMemoryFunctionLocation
10.0136353208{main}( )..\Index.php:0
20.0137353312parseURL( string(45) )..\Index.php:77
30.0137354792 implode ( string(2), array(2) )..\Index.php:66

a<>Array

这就说明implode不会处理嵌套数组。

 

如果数组中只有一个元素呢,继续看例子

<?php
	$arr = array('a');
	print_r(implode('<>', $arr));
?>

 输出 a

这是因为就一个元素,就用不着$glue粘了

 

其实php也有join函数,功能就是implode,如果习惯了JS中使用,那就使用join吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值