Home > Backend Development > PHP Tutorial > Convert php two-dimensional array to string

Convert php two-dimensional array to string

WBOY
Release: 2016-07-30 13:31:27
Original
1407 people have browsed it
<pre name="code" class="php">//二维数组转化为字符串,中间用,隔开
function arr_to_str($arr){
    foreach ($arr as $v){
        $v = join(",",$v); //可以用implode将一维数组转换为用逗号连接的字符串,join是别名
        $temp[] = $v;
    }
    foreach($temp as $v){
        $t.=$v.",";
    }
    $t=substr($t,0,-1);  //利用字符串截取函数消除最后一个逗号
    return $t;
}
Copy after login
Copy after login

PS:

substr application

$str = "1,2,3,4,5,6,";
$newstr = substr($str,0,strlen($str)-1);
echo $newstr;
Copy after login
The function that comes with the system can also achieve this effect, two methods:
1) substr($str, 0, -1);
2)rtrim($ str, ",")

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the conversion of two-dimensional arrays into strings in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template