本文主要介紹了php中array_slice和array_splice函數(shù),感興趣的可以圍觀一下,
array_slice和array_splice函數(shù)是用在取出數(shù)組的一段切片,array_splice還有用新的切片替換原刪除切片位置的功能。類似javascript中的Array.prototype.splice和Array.prototype.slice方法。
array_slice
1
|
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í)例
1
2
3
4
5
6
7
8
9
|
<?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
1
|
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()去包著它。
使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?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中,有這么一段代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/* 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ì)本站的支持!