Home > php教程 > php手册 > body text

php中字符串转换成数组函数explode(),implode()

WBOY
Release: 2016-06-13 09:48:05
Original
864 people have browsed it

在php中数组与字符串相互转换最常用的方法就是使用explode(),implode()函数来转换了,今天我看一朋友问了一个这样的问题,下面我来整理一下分享给各位。

行看看这两个函数

implode 函数:

使用函数 implode 将数组转换为字符串

explode 函数:

使用函数 explode 将字符串转换成数组

例1.

今天在php论坛中看到以为朋友发了一个帖子请教php如何把字符串转换成数组;作为php程序员的新一第一反应是联想到explode(),implode()这两个函数。新一也是用里面的函数进行转换成数组的。

con[1]=28&selt[1]=1&con[2]=29&selt[2]=4&con[3]=26&selt[3]=4

&con[4]=30&selt[4]=2&con[5]=4&selt[5]=1&con[6]=11&con[7]=12

上面就是网友需要转换成PHP数组的字符串;下面也是新一提供PHP转换代码

 代码如下 复制代码

$str = 'con[1]=28&selt[1]=1&con[2]=29&selt[2]=4&con[3]=26&selt[3]=4&con[4]=30&selt[4]=2&con[5]=4&selt[5]=1&con[6]=11&con[7]=12';
$arr = explode('&',$str);
$arr2 = array();
foreach($arr as $k=>$v){
        $arr = explode('=',$v);
        $arr2[$k] = $arr[1];
}
print_r($arr2);
?>
//输出
Array
(
    [0] => 28
    [1] => 1
    [2] => 29
    [3] => 4
    [4] => 26
    [5] => 4
    [6] => 30
    [7] => 2
    [8] => 4
    [9] => 1
    [10] => 11
    [11] => 12
)
//第二个
$str = 'con[1]=28&selt[1]=1&con[2]=29&selt[2]=4&con[3]=26&selt[3]=4&con[4]=30&selt[4]=2&con[5]=4&selt[5]=1&con[6]=11&con[7]=12';
$arr = explode('&',$str);
$arr2 = array();
foreach($arr as $k=>$v){
        $arr = explode('=',$v);
        $arr2[$arr[0]] = $arr[1];
}
print_r($arr2);
?>
//输出
Array
(
    [con[1]] => 28
    [selt[1]] => 1
    [con[2]] => 29
    [selt[2]] => 4
    [con[3]] => 26
    [selt[3]] => 4
    [con[4]] => 30
    [selt[4]] => 2
    [con[5]] => 4
    [selt[5]] => 1
    [con[6]] => 11
    [con[7]] => 12
)


例2.
上面只支持一维数据,如果是二维或三维数据呢

 代码如下 复制代码


//将多维数组中所有的数值转换成字符串????》最多支持三维数组
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 );
}


例3.

将数组转成字符串存储及字符串取出转成数组(serialize & unserialize)

下面看一个实例:

 代码如下 复制代码

// $Id: test3.php,v 1.0 2011-6-7  goba Exp $
 
/**
 * 测试PHP存储数值
 *
 * @author Lok
 */
 
 
$config = array (
 'host' => 'localhost',
 'user_name' => 'root',
 'password' => '',
 'db_name' => 'ecshop',
 'charset' => 'utf8',
 'remark' => array ('note' => '要注意密码的安全', 'author' => 'Lok')
);
 
 
$str = serialize($config);
$arr = unserialize($str);
 
echo '--------str--------
';
var_dump($str);
echo '
-------arr----------
';
var_dump($arr)
?>结果:

--------str--------
string(221) "a:6:{s:4:"host";s:9:"localhost";
   s:9:"user_name";s:4:"root";
   s:8:"password";s:0:"";
   s:7:"db_name";s:8:"ecshop";
   s:7:"charset";s:4:"utf8";
   s:6:"remark";a:2:{s:4:"note";s:24:"要注意密码的安全";
                s:6:"author";s:3:"Lok";}}"
-------arr----------
array(6) { ["host"]=> string(9) "localhost"
        ["user_name"]=> string(4) "root"
        ["password"]=> string(0) ""
        ["db_name"]=> string(8) "emoishop"
               ["charset"]=> string(4) "utf8"
               ["remark"]=> array(2) { ["note"]=> string(24) "要注意密码的安全"
             ["author"]=> string(3) "Lok" } }

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template