PHP中explode和implode的使用

explode() 函数把字符串分割为数组;

implode() 函数把数组元素组合为一个字符串。

explode

定义 array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) 
参数: limit 限制结果的个数。

$str = "hi, jason, world";
print_r(explode(',', $str));
print_r(explode(',', $str, 2));

结果:

Array
(
[0] => hi
[1] => jason
[2] => world
)
Array
(
[0] => hi
[1] => jason, world
)

 练习题例子:定义一个字符串a,计算出下划线后面的值相加得多少,使用分割字符串函数。

 $a='331612_1,331495_1,331461_2,331559_1,333080_1,333555_1,331448_1,331618_1,333388_1,33
 3408_1,327937_1,331615_1,331499_1';
 $arr=explode(',',$a);
 $sum=0;
 foreach ($arr as $value) {
     $ary=explode('_', $value);
     $sum+=$ary[1];
 }
 print_r($sum);

implode

定义 string implode ( string $glue , array $pieces )

<?php

$array  = array( 'lastname' ,  'email' ,  'phone' );
 $comma_separated  =  implode ( "," ,  $array );

echo  $comma_separated ;  // lastname,email,phone

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值