1.implode将数组元素组合为字符串
连接符为 ^ 的例子:
<?php
$array = array('姓名', '电话', '电子邮箱');
$char = implode("^", $array);
echo $char;
?>
浏览器输出:
姓名^电话^电子邮箱
可以使用空格作为连接符:
<?php
$char = implode(" ", $array);
?>
2.自定义函数
function array2string($data, $isformdata = 1) {
if($data == '') return '';
//if($isformdata) $data = stripslashes($data);
return addslashes(var_export($data, TRUE));
}
字符转换成数组
例子 1. explode() 示例
<?php
// 示例 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// 示例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
例子 2. limit 参数示例
<?php
$str = 'one|two|three|four';
// 正数的 limit
print_r(explode('|', $str, 2));
// 负数的 limit
print_r(explode('|', $str, -1));
?>
以上示例将输出:
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)