php implode函数 多维数组

作者:matrix 发布时间:2015-03-19 分类:PHP

PHP implode()[别名join]的作用是将数组元素拼接成一个字符串。
$arr=array('a','b',array('1','2'),'c'); //二维数组
$s=implode(',',$arr);
//返回a,b,Array,c
但是遇到上面这种多维数组这样的implode()就没办法处理。
I was a little worried about the multi-dimensional array implodes listed here, as they are using 'for' loops, which is bad programming practice as arrays are not always nice and neat.

I hope this helps
好在早有解决方案:

<?php  
function multi_implode($glue, $pieces)  
{  
    $string='';  

    if(is_array($pieces))  
    {  
        reset($pieces);  
        while(list($key,$value)=each($pieces))  
        {  
            $string.=$glue.multi_implode($glue, $value);  
        }  
    }  
    else  
    {  
        return $pieces;  
    }  

    return trim($string, $glue);  
}  

说明:和implode()使用参数一样。multi_implode(字符, 数组)
参考:
http://php.chinaunix.net/manual/zh/function.implode.php#94688
http://g.xker.com/97041.html
http://blog.sina.cn/dpool/blog/s/blog_50a1e1740101aet6.html?vt=4