php implode函数 多维数组

作者:matrix 被围观: 4,396 次 发布时间:2015-03-19 分类:PHP | 8 条评论 »

这是一个创建于 3325 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

其他文章:
本文固定链接:https://www.hhtjim.com/php-implode-function-multidimensional-arrays.html
matrix
本文章由 matrix 于2015年03月19日发布在PHP分类下,目前没有通告,你可以至底部留下评论。
转载请注明:php implode函数 多维数组-HHTjim'S 部落格
关键字:,

有8 条评论 »

  1. 上海SEO小赛 上海SEO小赛 2015-4-30 17:22:19 +0800#5

    😛 😛 貌似略显复杂的!!

    • Matrix Matrix Moderator 2015-4-30 21:20:07 +0800

      理解 就好了 了解些PHP有时候还是有用的

  2. 111 111 2015-4-17 11:41:25 +0800#4

    怎么和java一样,if,while,else,中是如果,循环,否则,难道php和java一样???

    • Matrix Matrix Moderator 2015-4-17 16:51:14 +0800

      java不了解 差不多吧

  3. 消灭星星 消灭星星 2015-4-11 21:12:28 +0800#3

    交换友链 http://huanglv.me/p 消灭星星

    • Matrix Matrix Moderator 2015-4-12 10:25:00 +0800

      不换了 谢谢。

  4. 快乐淘 快乐淘 2015-4-7 15:20:34 +0800#2

    😯 😯
    额,
    看不太懂耶~

  5. 灰常记忆 灰常记忆 2015-4-5 8:43:09 +0800#1

    🙁 看不懂 我之是占位的 😛

添加新评论 »

 🙈 😱 😂 😛 😭 😳 😀 😆 👿 😉 😯 😮 😕 😎 😐 😥 😡 😈 💡

插入图片

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!