[PHP source reading]array_slice and Array_splice functions

Source: Internet
Author: User

The Array_slice and Array_splice functions are used to remove a slice of an array, and Array_splice also has the ability to replace the original deleted slice position with a new slice. Similar to the Array.prototype.splice and Array.prototype.slice methods in JavaScript.

I have a more detailed comment on the PHP source on GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. Comments that have been added can be viewed through a commit record.

Array_slice

Array array_slice (array $array, int $offset [, int $length = NULL [, bool $preserve _keys = false]])

Returns a sub-array slice that specifies the subscript offset and length of the array.

Parameter description

Sets the length of the first parameter array to num_in.

Offset

If offset is positive and less than length, the returned array starts with offset, and if offset is greater than length, it is not manipulated and is returned directly. If offset is a negative number, offset = Num_in+offset, and if Num_in+offset = = 0, offset is set to 0.

Length

If length is less than 0, then length is converted to Num_in-offset + length; otherwise, if Offset+length > array_count, then length = Num_in-offset. If the length is less than 0 after processing, it is returned directly.

Preserve_keys

The default is false, which does not preserve the original order of numeric key values, and sets true to preserve the order of the array's original numeric key values.

Working with instances
<?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 ');
Run steps

Processing parameters: Offset, length

Move the pointer to the position where offset points

Copy the length element to the return array starting with offset

Run the flowchart as follows

Array_splice

Array Array_splice (array & $input, int $offset [, int $length = 0 [, Mixed $replacement = Array ()]])

Delete the length element from offset in input, and replace the deleted element with the replacement array if there is a replacement argument.

Parameter description

The offset and length parameters in the Array_splice function are the same as in the Array_slice function.

Replacement

If this parameter is set, then the function will be replaced with the replacement array.

If offset and length specify that no elements need to be removed, then the replacement is inserted into the offset position.

If replacement has only one element, it can be wrapped without an array ().

Using the example
<?PHP$input=Array("Red", "green", "blue", "yellow"));Array_splice($input, 2);//$input into an array ("Red", "green")$input=Array("Red", "green", "blue", "yellow"));Array_splice($input, 1,-1);//$input into an array ("Red", "yellow")$input=Array("Red", "green", "blue", "yellow"));Array_splice($input, 1,Count($input), "Orange");//$input into an array ("Red", "orange")$input=Array("Red", "green", "blue", "yellow"));Array_splice($input,-1, 1,Array("Black", "maroon"));//$input as Array ("Red", "green",//"Blue", "Black", "maroon")$input=Array("Red", "green", "blue", "yellow"));Array_splice($input, 3, 0, "purple");//$input for Array ("Red", "green",//"Blue", "purple", "yellow");

SOURCE Interpretation

In Array_splice, there is this piece of code:

    /*Don ' t create the array of removed elements if it's not going * to is used; e.g. only removing and/or replacing El Ements*/    if(return_value_used) {//create return array if useful to function return value, otherwise do not create return array        intSize =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(((UnsignedLong) offset + (unsignedLong) 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); }

The Array_splice function returns the slice that was deleted. This code means that if Array_splice needs to return a value, the returned array is created, or not created, to avoid wasting space. This is also a programming trick that returns only when needed. For example, using $result = Array_splice (...) in a function, then return_value_used is true.

Summarize

To this end of this article, in peacetime programming, should be like the two functions when the implementation of the same approach, the most special situation first disposed of, and then continue, so as not to make redundant judgments, there is a need to save new variables to apply for new space, otherwise it will cause waste.

Original article, writing Limited, Caishuxueqian, if there is not in the text, million hope to inform.

If this article is helpful to you, please click on the recommendation, thank you ^_^

Finally Amway again, I have a more detailed comment on the PHP source code in GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. Comments that have been added can be viewed through a commit record.

More source articles, welcome to personal homepage Continue to view: Hoohack

[PHP source reading]array_slice and Array_splice functions

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.