[PHP源碼閱讀]array_slice和array_splice函數,slicesplice_PHP教程

來源:互聯網
上載者:User

[PHP源碼閱讀]array_slice和array_splice函數,slicesplice


array_slice和array_splice函數是用在取出數組的一段切片,array_splice還有用新的切片替換原刪除切片位置的功能。類似javascript中的Array.prototype.splice和Array.prototype.slice方法。

我在github上有對PHP源碼更詳細的註解。感興趣的可以圍觀一下,給個star。PHP5.4源碼註解。可以通過commit記錄查看已添加的註解。

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個元素到返回數組

運行流程圖如下

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。

總結

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

原創文章,文筆有限,才疏學淺,文中若有不正之處,萬望告知。

如果本文對你有協助,請點下推薦吧,謝謝^_^

最後再安利一下,我在github有對PHP源碼更詳細的註解。感興趣的可以圍觀一下,給個star。PHP5.4源碼註解。可以通過commit記錄查看已添加的註解。

更多源碼文章,歡迎訪問個人首頁繼續查看:hoohack

http://www.bkjia.com/PHPjc/1132198.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1132198.htmlTechArticle[PHP源碼閱讀]array_slice和array_splice函數,slicesplice array_slice和array_splice函數是用在取出數組的一段切片,array_splice還有用新的切片替換原刪除...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.