php中array_slice和array_splice函數解析_php技巧

來源:互聯網
上載者:User

本文主要介紹了php中array_slice和array_splice函數,感興趣的可以圍觀一下,

array_slice和array_splice函數是用在取出數組的一段切片,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 ]] )

返回數組中指定下標offset和長度length的子數組切片。

參數說明
設第一個參數數組的長度為num_in。

offset

如果offset是正數且小於length,則返回數組會從offset開始;如果offset大於length,則不操作,直接返回。如果offset是負數,則offset = num_in+offset,如果num_in+offset == 0,則將offset設為0。

length

如果length小於0,那麼會將length轉為num_in - offset + length;否則,如果offset+length > array_count,則length = num_in - offset。如果處理後length還是小於0,則直接返回。

preserve_keys

預設是false,預設不保留數字索引值原順序,設為true的話會保留數組原來的數字索引值順序。

使用執行個體

<?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');

運行步驟

  • 處理參數:offset、length
  • 移動指標到offset指向的位置
  • 從offset開始,拷貝length個元素到返回數組

運行流程圖如下

array_splice

array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement = array() ]] )

刪除input中從offset開始length個元素,如果有replacement參數的話用replacement數組替換刪除掉的元素。

參數說明

 array_splice函數中的offset和length參數跟array_slice函數中的用法一樣。

replacement

  • 如果這個參數設定了,那麼函數將使用replacement數組來替換。
  • 如果offset和length指定了沒有任何元素需要移除,那麼replacement會被插入到offset的位置。
  • 如果replacement只有一個元素,可以不用array()去包著它。

使用樣本

<?php$input = array("red", "green", "blue", "yellow");array_splice($input, 2);// $input變為 array("red", "green")$input = array("red", "green", "blue", "yellow");array_splice($input, 1, -1);// $input變為 array("red", "yellow")$input = array("red", "green", "blue", "yellow");array_splice($input, 1, count($input), "orange");// $input變為 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) { // 如果有用到函數傳回值則建立返回數組,否則不建立返回數組    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函數返回的是被刪除的切片。這段代碼的意思是,如果array_splice需要傳回值,那麼才建立返回數組,否則不建立,以免浪費空間。這也是一個編程小技巧,僅當需要的時候才返回。比如在函數中使用$result = array_splice(...),那麼return_value_used就是true。

總結

到此本文結束,在平時編程中,應當像這兩個函數實現時的做法一樣,將最特殊的情況先處理掉,然後再繼續,以免做了多餘的判斷;有需要儲存新變數的時候才申請新的空間,不然會造成浪費。

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.