ddlddx0000 发表于 2017-3-21 12:18:55

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

#
Time
Memory
Function
Location


1
0.0136
353208
{main}( )
..\Index.php:0


2
0.0137
353312
parseURL( string(45) )
..\Index.php:77


3
0.0137
354792

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吧
页: [1]
查看完整版本: php字符串函数implode