search
Homephp教程php手册使用 implode()函数怎么将多维数组转成字符串的实现方法

使用 implode()函数怎么将多维数组转成字符串的实现方法

Jun 02, 2017 am 11:30 AM
implodephpMultidimensional Arrays

 PHP implode()的用法和explode()类似,但是功能却恰恰相反,implode()是将数组元素组合成一个字符串。

    <?php
    $arr=array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;);
    $s=implode($arr);  //默认是以空字符合并数组元素
    //$s=implode(&#39;,&#39;,$arr);  //指定以","合并数组元素
    echo $s;
    ?>

上面这是一个简单的一维数组,那么多维数组行不行呢?

    <?php
    $arr=array(&#39;a&#39;,&#39;b&#39;,array(&#39;4&#39;,&#39;5&#39;,&#39;6&#39;),&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,array(&#39;1&#39;,&#39;2&#39;,&#39;3&#39;));  //定义一个二维数组
    $s=implode(&#39;,&#39;,$arr);
    echo $s;
    ?>

 得到的结果是:

    a,b,Array,c,d,e,Array

很显然,对于多维数组,implode()函数就无能为力了!那么多维数组怎样转字符串呢?如果先将多维数组转成一维数组,再通过implode()函数合并成字符串,不就可以完成了吗!关于多维数组转一维数组可以参考这篇文章PHP多维数组转一维数组的简单实现方法

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools