Home  >  Article  >  Backend Development  >  改写函数实现PHP二维/三维数组转字符串_php技巧

改写函数实现PHP二维/三维数组转字符串_php技巧

WBOY
WBOYOriginal
2016-05-17 08:55:21854browse

由于工作需要,自己在手册给定的示例函数基础上改写出了这样一个函数,代码如下:

复制代码 代码如下:

//将多维数组中所有的数值转换成字符串————》最多支持三维数组
function implodex( $glue, $array, $separator='' ) {
if ( ! is_array( $array ) ) return $array;
$string = array();

$count = 0;
foreach ( $array as $key => $val ) {
if ( is_array( $val ) )
$val = implode( $glue, $val );

if($count == 0){
$string[] = "{$val}";
}else{
$string[] = "{$glue}{$val}";
}
}

if(empty($separator))$separator = $glue;

return implode( $separator, $string );
}
Statement:
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