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)文章
php調(diào)用方法mssql_fetch_row、mssql_fetch_array、mssql_fetch_assoc和m
mysql_fetch_object() 和 mssql_fetch_array類似,只有一點(diǎn)區(qū)別,即返回一個(gè)對(duì)象而不是數(shù)組,并將字段名字做為屬性。間接地也意味著只能通過字段名來訪問,而不是偏移量2012-08-08php實(shí)現(xiàn)的驗(yàn)證碼文件類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的驗(yàn)證碼文件類,實(shí)例分析了php生成驗(yàn)證碼文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-06-06PHP統(tǒng)計(jì)數(shù)值數(shù)組中出現(xiàn)頻率最多的10個(gè)數(shù)字的方法
這篇文章主要介紹了PHP統(tǒng)計(jì)數(shù)值數(shù)組中出現(xiàn)頻率最多的10個(gè)數(shù)字的方法,涉及php中array_count_values與arsort等方法的相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04php簡(jiǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù)
這篇文章主要介紹了php簡(jiǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù),并附上2則示例代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-04-04php中的標(biāo)量數(shù)據(jù)類型總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于php中的標(biāo)量數(shù)據(jù)類型總結(jié)內(nèi)容,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2022-01-01PHP empty函數(shù)報(bào)錯(cuò)解決辦法
這篇文章主要介紹了PHP empty函數(shù)在檢測(cè)一個(gè)非變量情況下報(bào)錯(cuò)的解決辦法,需要的朋友可以參考下2014-03-03