Talking about PHP source code 21: About Array_slice function

Source: Internet
Author: User
This article mainly introduces about the PHP source code 21: Array_slice function, has a certain reference value, now share to everyone, the need for friends can refer to

Talking about PHP source code 21: Array_slice function

Array_slice
(PHP 4, PHP 5)

array_slice-remove a paragraph from an array
Description
Array array_slice (array array, int offset [, int length [, bool Preserve_keys]])

Array_slice () returns a sequence in an array of arrays specified by the offset and length parameters.

If offset is non-negative, the sequence starts at this offset in the array. If offset is negative, the sequence starts at a distance from the end of the array.

If length is given and is positive, then there will be so many cells in the sequence. If length is given and is negative, the sequence terminates so far away from the end of the array. If omitted, the sequence starts at offset from the end of the array.
This paragraph shows that the implementation in the program is very simple, as follows:

while (POS < offset + length && ...) { ....... }

Note Array_slice () defaults to reset the array's keys. From PHP 5.0.2, you can change this behavior by setting Preserve_keys to TRUE.

The key to reset the array, in this case, is to reset the number keys in the array.
The PHP code is as follows:

<? PHP $arr = Array (2 = 2, 1 = 1, ' s ' = = ' s ', ' 0 ' = ' ddd '); Var_dump (Array_slice ($arr, 0)); Die (); /* This program outputs: Array (4) {[0]=> int (2) [1]=> int (1) ["S"]=> string (1) "S" [2]=> string (3) "DDD"} */

In the source code we can see:

<pre line= "1" Switch (ZEND_HASH_GET_CURRENT_KEY_EX (z_arrval_p (input), &string_key, &string_key_len, & Num_key, 0, &hpos)) {case     hash_key_is_string:    //    key is a string, there is no operation to reset the array key         zend_hash_update (z_arrval _p (Return_value), String_key, String_key_len, entry, sizeof (Zval *), NULL);         break;     Case Hash_key_is_long:    //    key is numeric     if (preserve_keys) {    ////    Preserve_keys is true without resetting the numeric key         Zend _hash_index_update (Z_arrval_p (Return_value), Num_key, entry, sizeof (Zval *), NULL);     } else {    //    reset number keys by default         Zend_hash_next_index_insert (Z_arrval_p (return_value), entry, sizeof (Zval *), NULL);     }     Break }

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.