溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP中explode和implode的使用方法

發(fā)布時間:2020-10-19 16:25:03 來源:億速云 閱讀:152 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)PHP中explode和implode的使用方法的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

explode() 函數(shù)把字符串分割為數(shù)組;

implode() 函數(shù)把數(shù)組元素組合為一個字符串。

explode

定義 array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )

參數(shù): limit 限制結(jié)果的個數(shù)。

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

結(jié)果:

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

練習(xí)題例子:定義一個字符串a(chǎn),計算出下劃線后面的值相加得多少,使用分割字符串函數(shù)。

 $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

感謝各位的閱讀!關(guān)于PHP中explode和implode的使用方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI