溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php中array_slice和array_splice函數(shù)如何使用

發(fā)布時(shí)間:2021-07-14 16:02:26 來源:億速云 閱讀:207 作者:Leah 欄目:開發(fā)技術(shù)

php中array_slice和array_splice函數(shù)如何使用,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

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)行流程圖如下

php中array_slice和array_splice函數(shù)如何使用

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)?nbsp;array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input變?yōu)?nbsp;array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input變?yōu)?nbsp;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。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI