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 ]] )
返回數(shù)組中指定下標(biāo)offset和長度length的子數(shù)組切片。
參數(shù)說明
設(shè)第一個參數(shù)數(shù)組的長度為num_in。
offset
如果offset是正數(shù)且小于length,則返回數(shù)組會從offset開始;如果offset大于length,則不操作,直接返回。如果offset是負(fù)數(shù),則offset = num_in+offset,如果num_in+offset == 0,則將offset設(shè)為0。
length
如果length小于0,那么會將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的話會保留數(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');
運行步驟
- 處理參數(shù):offset、length
- 移動指針到offset指向的位置
- 從offset開始,拷貝length個元素到返回數(shù)組
運行流程圖如下

array_splice
array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement = array() ]] )
刪除input中從offset開始length個元素,如果有replacement參數(shù)的話用replacement數(shù)組替換刪除掉的元素。
參數(shù)說明
array_splice函數(shù)中的offset和length參數(shù)跟array_slice函數(shù)中的用法一樣。
replacement
- 如果這個參數(shù)設(shè)置了,那么函數(shù)將使用replacement數(shù)組來替換。
- 如果offset和length指定了沒有任何元素需要移除,那么replacement會被插入到offset的位置。
- 如果replacement只有一個元素,可以不用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)建返回數(shù)組,否則不創(chuàng)建返回數(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)建返回數(shù)組,否則不創(chuàng)建,以免浪費空間。這也是一個編程小技巧,僅當(dāng)需要的時候才返回。比如在函數(shù)中使用$result = array_splice(...),那么return_value_used就是true。
總結(jié)
到此本文結(jié)束,在平時編程中,應(yīng)當(dāng)像這兩個函數(shù)實現(xiàn)時的做法一樣,將最特殊的情況先處理掉,然后再繼續(xù),以免做了多余的判斷;有需要保存新變量的時候才申請新的空間,不然會造成浪費。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- php數(shù)組函數(shù)array_push()、array_pop()及array_shift()簡單用法示例
- PHP array_shift()用法實例分析
- php通過array_shift()函數(shù)移除數(shù)組第一個元素的方法
- PHP中unset,array_splice刪除數(shù)組中元素的區(qū)別
- 解析array splice的移除數(shù)組中指定鍵的值,返回一個新的數(shù)組
- php數(shù)組函數(shù)序列之a(chǎn)rray_splice() - 在數(shù)組任意位置插入元素
- js利用Array.splice實現(xiàn)Array的insert/remove
- Array.slice()與Array.splice()的返回值類型
- php去掉數(shù)組的第一個值的兩個函數(shù):array_shift、array_splice
相關(guān)文章
關(guān)于PHPDocument 代碼注釋規(guī)范的總結(jié)
本篇文章是對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用法實例
這篇文章主要介紹了php專用數(shù)組排序類ArraySortUtil用法,實例分析了ArraySortUtil實現(xiàn)數(shù)組排序的方法與對應(yīng)使用技巧,需要的朋友可以參考下2015-04-04
Ping服務(wù)的php實現(xiàn)方法,讓網(wǎng)站快速被收錄
這篇博文繼續(xù)說說這個ping服務(wù)的問題,首先歸納和總結(jié)以下資料2012-02-02
linux下使用crontab實現(xiàn)定時PHP計劃任務(wù)失敗的原因分析
這篇文章主要介紹了linux下使用crontab實現(xiàn)定時PHP計劃任務(wù)失敗的原因分析,需要的朋友可以參考下2014-07-07

