• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP数组和字符串转换(超详细

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、数组转字符串

implode

方法介绍:

function implode ($glue = "", array $pieces) {}

主要有两个参数 
一个是连接符(glue:胶水的意思),默认是空字符串 
一个是数组

使用

$test = array("hello","world","php");
echo implode("-",$test);

结果: 
hello-world-php 

$test = array("h"=>"hello","w"=>"world","p"=>"php");
echo implode("-",$test);

2、字符串分割成数组

2.1 按某个字符分割

function explode ($delimiter, $string, $limit = null) {}

explode 
参数解释:
delimiter,分隔符
limit
文档google翻译结果:
如果limit设置为正,则返回的数组将包含最大限制元素,最后一个元素包含字符串的其余部分。
limit 限制分割的份数,最后一份为剩下的字符串分割后剩下的,当然如果为1的话,就是字符串本身(已经过实验)
代码演示:

$str="hello-world-php";
$result = explode("-", $str);
var_dump($result);
$result = explode("-", $str,2);
var_dump($result);

输出结果:

array(3) {
  [0]=>
  string(5) "hello"
  [1]=>
  string(5) "world"
  [2]=>
  string(3) "php"
}
array(2) {
  [0]=>
  string(5) "hello"
  [1]=>
  string(9) "world-php"
}

2.2 按距离读取

可能字符串不用分割,但是需要把每个字符拿出来,也就是一个一个读出来 

**
 * Convert a string to an array
 * @link http://php.net/manual/en/function.str-split.php
 * @param string $string <p>
 * The input string.
 * </p>
 * @param int $split_length [optional] <p>
 * Maximum length of the chunk.
 * </p>
 * @return array If the optional split_length parameter is
 * specified, the returned array will be broken down into chunks with each
 * being split_length in length, otherwise each chunk
 * will be one character in length.
 * </p>
 * <p>
 * false is returned if split_length is less than 1.
 * If the split_length length exceeds the length of
 * string, the entire string is returned as the first
 * (and only) array element.
 * @since 5.0
 */
function str_split ($string, $split_length = 1) {}

数组如果指定了可选的split_length参数,则返回的数组将被分解为长度为split_length的块,否则每个块将是一个字符长度。 

$str = "hello";
var_dump(str_split($str,2));
1
2


array(3) {
  [0]=>
  string(2) "he"
  [1]=>
  string(2) "ll"
  [2]=>
  string(1) "o"
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
转:PHP环境搭建-Linux发布时间:2022-07-10
下一篇:
php设计模式之工厂模式发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap