[PHP Source Code Reading] array_slice and array_splice functions array_slice and array_splice are used to retrieve a segment of the array. array_splice also supports replacing the original position of the deleted slice with a new slice. Similar to the Array. prototype. splice and Array. prototype. slice methods in javascript.
I have a more detailed description of the PHP source code on github. If you are interested, you can look around and give a star. PHP5.4 source code annotation. You can use the commit record to view the added annotations.
Array_slice
Array array_slice (array $ array, int $ offset [, int $ length = NULL [, bool $ preserve_keys = false])
Returns the subarray slices with the specified subscript offset and length in the array.
Parameter description
Set the length of the first parameter array to num_in.
Offset
If the offset value is positive and smaller than the length value, the returned array starts with the offset value. If the offset value is greater than the length value, the system returns the value directly. If offset is negative, offset = num_in + offset. If num_in + offset = 0, offset is set to 0.
Length
If the length is less than 0, the length will be converted to num_in-offset + length; otherwise, if the offset + length> array_count, the length = num_in-offset. If the length after processing is still less than 0, the system returns directly.
Preserve_keys
The default value is false. The original sequence of numeric keys is not retained by default. If it is set to true, the original sequence of numeric keys is retained.
Use instance
'c', 1 => 'd');print_r(array_slice($input, 2, -1, true)); // array(2 => 'c', 1 => 'd');
Procedure
Processing Parameters: offset and length
Move the pointer to the position pointed to by offset
Starting from offset, copy the length element to the returned array.
The running flowchart is as follows:
Array_splice
Array array_splice (array & $ input, int $ offset [, int $ length = 0 [, mixed $ replacement = array ()])
Delete the length elements starting from offset in input. If the replacement parameter exists, replace the deleted element with the replacement array.
Parameter description
The offset and length parameters in the array_splice function are the same as those in the array_slice function.
Replacement
If this parameter is set, the function will replace it with the replacement array.
If offset and length specify that no elements need to be removed, the replacement will be inserted to the offset position.
If replacement has only one element, you do not need array () to enclose it.
Example
Source code explanationIn array_splice, there is such a piece of code:
/* 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) {// if function return values are useful, the returned array is created. Otherwise, the returned array int size = length is not created; /* 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-off Set + 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 );}The array_splice function returns the deleted slice. This Code indicates that if array_splice needs to return a value, the returned array is created. Otherwise, the array is not created to avoid space waste. This is also a programming tip, which is returned only when needed. For example, if $ result = array_splice (...) is used in a function, return_value_used is true.
SummaryAt the end of this article, in normal programming, we should process the most special case first, just like the practices of the two functions, and then continue, to avoid unnecessary judgment; apply for new space only when new variables need to be saved; otherwise, it will cause waste.
The original article is limited in writing, so it is easy to learn. If there is anything wrong with the article, please let us know.
If this article is helpful to you, please click here for recommendations. Thank you ^_^
At last, I have a more detailed description of the PHP source code on github. If you are interested, you can look around and give a star. PHP5.4 source code annotation. You can use the commit record to view the added annotations.
For more source code articles, visit the personal homepage to continue viewing: hoohack