談PHP中漢字替換與模式匹配的問(wèn)題_PHP教程
推薦:怎樣用PHP程序計(jì)算時(shí)間差在php中計(jì)算時(shí)間差有時(shí)候是件麻煩的事!不過(guò)只要你掌握了日期時(shí)間函數(shù)的用法那這些也就變的簡(jiǎn)單了:一個(gè)簡(jiǎn)單的例子就是計(jì)算借書(shū)的天數(shù),這需要php根據(jù)每天的日期進(jìn)行計(jì)算,下面就來(lái)談?wù)剬?shí)現(xiàn)這種日期計(jì)算的幾種方法: (1) 如果有數(shù)據(jù)庫(kù)就很容易了!若是MSSQL可以使
談PHP中漢字替換與模式匹配的問(wèn)題,PHP 4.0中新添加了30個(gè)與組數(shù)有關(guān)的函數(shù),其中一些常見(jiàn)的函數(shù)可以判斷一個(gè)數(shù)組中是否包含某個(gè)元素,對(duì)一個(gè)數(shù)組中的元素進(jìn)行計(jì)數(shù),添加或刪除數(shù)組中的元素或者對(duì)數(shù)組中的元素進(jìn)行排序。
如果有一個(gè)很大的數(shù)組,而你需要找出其中是否包含一個(gè)特定的元素,就可以使用in_array()。下面的例子將顯示“Not found in this array”,因?yàn)樵谝粋(gè)名字為$namesArray的數(shù)組中查找Albert,而在$namesArray數(shù)組中不存在這樣一個(gè)元素。
$lookingFor = "Albert";
if (in_array($lookingFor, $namesArray)) {
echo "You've found it!";
} else {
echo "Not found in this array!";
}
?>
如果把$lookingFor的值改為Mary,就會(huì)得到“You've found it!”的信息,因?yàn)镸ary是$namesArray數(shù)組中的一個(gè)元素。
如果要對(duì)一個(gè)數(shù)組中的元素個(gè)數(shù)進(jìn)行計(jì)數(shù),只要簡(jiǎn)單地使用count()函數(shù)即可:
$count = count($namesArray); ?>
返回的$count的值為7。
可以在一個(gè)數(shù)組的開(kāi)頭或結(jié)尾處添加元素,還可以使用array_merge()來(lái)建立一個(gè)包含二個(gè)或更多數(shù)組中元素的新數(shù)組,合并時(shí),元素的順序會(huì)按指定的順序排列,如果原來(lái)的數(shù)組是被排過(guò)序的,在合并后需要對(duì)它重新排序。
我們可以首先利用array_push()在數(shù)組的結(jié)尾處添加一個(gè)元素:
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 向數(shù)組中添加元素 */
array_push($fruitArray, "grape", "pineapple", "tomato");
/*顯示每個(gè)元素及其序號(hào)*/
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value
";
}
?>
運(yùn)行上面的程序?qū)⒌玫较旅娴慕Y(jié)果:
0 : apple
1 : orange
2 : banana
3 : kiwi
4 : pear
5 : grape
6 : pineapple
7 : tomato
如果需要在數(shù)組的開(kāi)頭添加元素,其代碼與上面的代碼差不多,唯一的不同之處是需要用array_unshift()代替array_push()。
/* 建立一個(gè)數(shù)組*/
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 向數(shù)組中添加元素*/
array_unshift($fruitArray, "grape", "pineapple", "tomato");
/* 顯示每個(gè)元素及其序號(hào)*/
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value
";
}
?>
運(yùn)行上面的程序?qū)⒌玫较旅娴慕Y(jié)果:
0 : grape
1 : pineapple
2 : tomato
3 : apple
4 : orange
5 : banana
6 : kiwi
7 : pear
array_merge()函數(shù)可以把二個(gè)或更多的數(shù)組合并為一個(gè)數(shù)組。
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/*/建立第二個(gè)數(shù)組*/
$vegArray = array("carrot", "green beans", "asparagus", "artichoke", "corn");
/*把這二個(gè)數(shù)組合并為一個(gè)數(shù)組*/
$goodfoodArray = array_merge($fruitArray, $vegArray);
/* 顯示每個(gè)元素及其序號(hào)*/
while (list($key,$value) = each($goodfoodArray)) {
echo "$key : $value
";
}
?>
運(yùn)行上面的腳本將得到下面的結(jié)果:
0 : apple
1 : orange
2 : banana
3 : kiwi
4 : pear
5 : carrot
6 : green beans
7 : asparagus
8 : artichoke
9 : corn
現(xiàn)在我們已經(jīng)掌握了如何添加元素和合并數(shù)組,我們?cè)賮?lái)看看如何從一個(gè)數(shù)組中刪除元素。從一個(gè)數(shù)組的末尾刪除一個(gè)元素可以使用array_pop()函數(shù),使用array_shift()函數(shù)可以從一個(gè)數(shù)組的開(kāi)頭刪除一個(gè)元素。盡管使用array_pop()或 array_shift()從數(shù)組中刪除了一個(gè)元素,你還可以把這個(gè)元素當(dāng)作一個(gè)變量來(lái)使用。
使用array_pop()從一個(gè)數(shù)組的末尾刪除一個(gè)元素:
/*建立一個(gè)數(shù)組*/
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 從數(shù)組的末尾刪除一個(gè)元素*/
$popped = array_pop($fruitArray);
/* 顯示刪除后數(shù)組的內(nèi)容和你刪除的元素*/
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value
";
}
echo "
and finally, in $popped: $popped";
?>
運(yùn)行上面的腳本會(huì)得到下面的結(jié)果:
0 : apple
1 : orange
2 : banana
3 : kiwi
and finally, in $popped: pear
我們?cè)賮?lái)討論一個(gè)從一個(gè)數(shù)組的末尾刪除元素的例子:
/* 建立一個(gè)數(shù)組*/
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/*從一個(gè)數(shù)組的開(kāi)始刪除一個(gè)元素*/
$shifted = array_shift($fruitArray);
/* 顯示刪除后數(shù)組的內(nèi)容和你刪除的元素*/
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value
";
}
echo "
and finally, in $shifted: $shifted";
?>
運(yùn)行上述腳本會(huì)得到如下的顯示結(jié)果:
0 : orange
1 : banana
2 : kiwi
3 : pear
and finally, in $shifted: apple
另外還有幾個(gè)函數(shù)可以對(duì)數(shù)組中的元素進(jìn)行排序,但在這里我們將只簡(jiǎn)要介紹基本的排序函數(shù),說(shuō)明排序的過(guò)程:
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 對(duì)數(shù)組進(jìn)行排序*/
sort($fruitArray);
/*顯示每個(gè)元素及其序號(hào)*/
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value
";
}
?>
運(yùn)行上述的腳本會(huì)得到如下的顯示結(jié)果:
0 : apple
1 : banana
2 : kiwi
3 : orange
4 : pear
分享:用PHP自定義錯(cuò)誤處理器處理出錯(cuò)信息如果您是PHP老手,當(dāng)然知道當(dāng)PHP腳本出錯(cuò)時(shí)發(fā)生了什么事情。此時(shí)PHP解析器將在屏幕上給出錯(cuò)誤信息,如 Fatal error: Call to undefined function on line 19 --,因此程序在此處終止。這個(gè)信息會(huì)嚇到客戶(hù),他可能立即打電話(huà)和你進(jìn)行咨詢(xún)。 幸運(yùn)的是,這里有
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪(fǎng)問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語(yǔ)言構(gòu)造器介紹
- php/js獲取客戶(hù)端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語(yǔ)言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- PHP實(shí)例:上傳多個(gè)圖片并校驗(yàn)的代碼
- 基于PHP常用函數(shù)的用法詳解
- 淺析PHP安裝擴(kuò)展mcrypt以及相關(guān)依賴(lài)項(xiàng)(PHP安裝PECL擴(kuò)展的方法)
- php快速u(mài)rl重寫(xiě)實(shí)例
- 揭秘目前最好的PHP開(kāi)發(fā)框架
- PHP編程新手必看
- 解讀PHP數(shù)組讀取的操作
- php使用curl模擬用戶(hù)登陸
- PHP實(shí)用:用PHP來(lái)實(shí)現(xiàn)圖片的簡(jiǎn)單上傳
- php筆記之:php函數(shù)range() round()和list()的使用說(shuō)明
- 相關(guān)鏈接:
- 教程說(shuō)明:
PHP教程-談PHP中漢字替換與模式匹配的問(wèn)題
。