php中array_slice和array_splice函數(shù)解析
本文主要介紹了php中array_slice和array_splice函數(shù),感興趣的可以圍觀一下,
array_slice和array_splice函數(shù)是用在取出數(shù)組的一段切片,array_splice還有用新的切片替換原刪除切片位置的功能。類似javascript中的Array.prototype.splice和Array.prototype.slice方法。
array_slice
array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )
返回?cái)?shù)組中指定下標(biāo)offset和長(zhǎng)度length的子數(shù)組切片。
參數(shù)說明
設(shè)第一個(gè)參數(shù)數(shù)組的長(zhǎng)度為num_in。
offset
如果offset是正數(shù)且小于length,則返回?cái)?shù)組會(huì)從offset開始;如果offset大于length,則不操作,直接返回。如果offset是負(fù)數(shù),則offset = num_in+offset,如果num_in+offset == 0,則將offset設(shè)為0。
length
如果length小于0,那么會(huì)將length轉(zhuǎn)為num_in - offset + length;否則,如果offset+length > array_count,則length = num_in - offset。如果處理后length還是小于0,則直接返回。
preserve_keys
默認(rèn)是false,默認(rèn)不保留數(shù)字鍵值原順序,設(shè)為true的話會(huì)保留數(shù)組原來的數(shù)字鍵值順序。
使用實(shí)例
<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
print_r(array_slice($input, 2, -1)); // array(0 => 'c', 1 => 'd');
print_r(array_slice($input, 2, -1, true)); // array(2 => 'c', 1 => 'd');
運(yùn)行步驟
- 處理參數(shù):offset、length
- 移動(dòng)指針到offset指向的位置
- 從offset開始,拷貝length個(gè)元素到返回?cái)?shù)組
運(yùn)行流程圖如下

array_splice
array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement = array() ]] )
刪除input中從offset開始length個(gè)元素,如果有replacement參數(shù)的話用replacement數(shù)組替換刪除掉的元素。
參數(shù)說明
array_splice函數(shù)中的offset和length參數(shù)跟array_slice函數(shù)中的用法一樣。
replacement
- 如果這個(gè)參數(shù)設(shè)置了,那么函數(shù)將使用replacement數(shù)組來替換。
- 如果offset和length指定了沒有任何元素需要移除,那么replacement會(huì)被插入到offset的位置。
- 如果replacement只有一個(gè)元素,可以不用array()去包著它。
使用示例
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input變?yōu)?array("red", "green")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input變?yōu)?array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input變?yōu)?array("red", "orange")
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input為 array("red", "green",
// "blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input為 array("red", "green",
// "blue", "purple", "yellow");
源碼解讀
在array_splice中,有這么一段代碼:
/* Don't create the array of removed elements if it's not going
* to be used; e.g. only removing and/or replacing elements */
if (return_value_used) { // 如果有用到函數(shù)返回值則創(chuàng)建返回?cái)?shù)組,否則不創(chuàng)建返回?cái)?shù)組
int size = length;
/* Clamp the offset.. */
if (offset > num_in) {
offset = num_in;
} else if (offset < 0 && (offset = (num_in + offset)) < 0) {
offset = 0;
}
/* ..and the length */
if (length < 0) {
size = num_in - offset + length;
} else if (((unsigned long) offset + (unsigned long) length) > (unsigned) num_in) {
size = num_in - offset;
}
/* Initialize return value */
array_init_size(return_value, size > 0 ? size : 0);
rem_hash = &Z_ARRVAL_P(return_value);
}
array_splice函數(shù)返回的是被刪除的切片。這段代碼的意思是,如果array_splice需要返回值,那么才創(chuàng)建返回?cái)?shù)組,否則不創(chuàng)建,以免浪費(fèi)空間。這也是一個(gè)編程小技巧,僅當(dāng)需要的時(shí)候才返回。比如在函數(shù)中使用$result = array_splice(...),那么return_value_used就是true。
總結(jié)
到此本文結(jié)束,在平時(shí)編程中,應(yīng)當(dāng)像這兩個(gè)函數(shù)實(shí)現(xiàn)時(shí)的做法一樣,將最特殊的情況先處理掉,然后再繼續(xù),以免做了多余的判斷;有需要保存新變量的時(shí)候才申請(qǐng)新的空間,不然會(huì)造成浪費(fèi)。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- php數(shù)組函數(shù)array_push()、array_pop()及array_shift()簡(jiǎn)單用法示例
- PHP array_shift()用法實(shí)例分析
- php通過array_shift()函數(shù)移除數(shù)組第一個(gè)元素的方法
- PHP中unset,array_splice刪除數(shù)組中元素的區(qū)別
- 解析array splice的移除數(shù)組中指定鍵的值,返回一個(gè)新的數(shù)組
- php數(shù)組函數(shù)序列之a(chǎn)rray_splice() - 在數(shù)組任意位置插入元素
- js利用Array.splice實(shí)現(xiàn)Array的insert/remove
- Array.slice()與Array.splice()的返回值類型
- php去掉數(shù)組的第一個(gè)值的兩個(gè)函數(shù):array_shift、array_splice
相關(guān)文章
關(guān)于PHPDocument 代碼注釋規(guī)范的總結(jié)
本篇文章是對(duì)PHPDocument代碼注釋規(guī)范進(jìn)行了詳細(xì)的總結(jié)與介紹,需要的朋友參考下2013-06-06
WordPress開發(fā)中的get_post_custom()函數(shù)使用解析
這篇文章主要介紹了WordPress開發(fā)中的get_post_custom()函數(shù)使用解析,get_post_custom()函數(shù)用于獲取自定義字段,需要的朋友可以參考下2016-01-01
php專用數(shù)組排序類ArraySortUtil用法實(shí)例
這篇文章主要介紹了php專用數(shù)組排序類ArraySortUtil用法,實(shí)例分析了ArraySortUtil實(shí)現(xiàn)數(shù)組排序的方法與對(duì)應(yīng)使用技巧,需要的朋友可以參考下2015-04-04
PHP實(shí)現(xiàn)判斷二叉樹是否對(duì)稱的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)判斷二叉樹是否對(duì)稱的方法,涉及php遞歸二叉樹判斷節(jié)點(diǎn)的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
PHP實(shí)現(xiàn)的博客歡迎提示功能(很特別哦)
很別致的歡迎詞功能,可以放在博客的空余位置,讓訪客通過直接訪問、搜索引擎訪問時(shí),顯示歡迎提示,就跟店門口的服務(wù)員說“歡迎光臨”似的,讓人如沐春風(fēng)啊。2014-06-06
Ping服務(wù)的php實(shí)現(xiàn)方法,讓網(wǎng)站快速被收錄
這篇博文繼續(xù)說說這個(gè)ping服務(wù)的問題,首先歸納和總結(jié)以下資料2012-02-02
linux下使用crontab實(shí)現(xiàn)定時(shí)PHP計(jì)劃任務(wù)失敗的原因分析
這篇文章主要介紹了linux下使用crontab實(shí)現(xiàn)定時(shí)PHP計(jì)劃任務(wù)失敗的原因分析,需要的朋友可以參考下2014-07-07
php實(shí)現(xiàn)的debug log日志操作類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的debug log日志操作類,結(jié)合實(shí)例形式分析了php針對(duì)日志的相關(guān)操作技巧,包括php數(shù)組、字符串及文件的寫操作等用法,需要的朋友可以參考下2016-07-07

