Talking about PHP source code 22: About Array_splice function

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

Talking about PHP source code 22: About Array_splice function
Array_splice
(PHP 4, PHP 5)

array_splice-remove part of the array and replace it with other values
Description
Array Array_splice (array &input, int offset [, int length [, array replacement]])

Array_splice () Removes the cells specified by offset and length from the input array, and if the replacement parameter is provided, it is substituted with the cells in the replacement array. Returns an array containing the cells that have been removed. Note the numeric key names in input are not retained.

If offset is positive, the offset specified by the value in the input array begins to be removed. If offset is negative, it is removed from the offset specified by the value of the countdown at the end of input.

If length is omitted, all parts of the array from offset to end are moved. If length is specified and positive, so many cells are removed. If length is specified and negative, all cells from offset to the end of the array ending with length are removed. Tip: When replacement is given to remove all cells from offset to the end of the array, count ($input) is used as length.

If the replacement array is given, the removed cell is replaced by the cells in this array. If the combination of offset and length does not remove any values, the cells in the replacement array are inserted into the position specified by offset. Note the key names in the replacement array are not preserved. If the value to replace is just a unit, then you do not need to add an array () to it, unless the cell itself is an array

The

Array_splice function calls the Php_splice function, and the main code for this function is described below:

 /* Creates and initializes the returned array, which returns an array of Php_splice, not the returned array of Array_splice (Array_splice returns an array of removed) */alloc_hashtable (Out_hash); Zend_hash_init (Out_hash, 0, NULL, zval_ptr_dtor, 0);/* Copies an array of elements from start to offset into the returned array */for (Pos=0, P=in_hash->plisthead ; Pos<offset && p;    pos++, p=p->plistnext) {entry = * ((zval * *) p->pdata); entry->refcount++;/* update returns an array of */if (p->nkeylength) zend_hash_quick_update (Out_hash, P->arkey, p-> Nkeylength, P->h, &entry, sizeof (Zval *), NULL), Else Zend_hash_next_index_insert (Out_hash, &entry, sizeof (Z Val *), NULL);} /* Assigns the element that needs to be removed to removed, which is the returned array of Array_splice */if (removed! = NULL) {for (; pos<offset+length && p; pos++, p=p    ->plistnext) {entry = * ((zval * *) p->pdata);    entry->refcount++; if (p->nkeylength) zend_hash_quick_update (*removed, P->arkey, P->nkeylength, p->h, &entry, sizeof (Z    Val *), NULL); else Zend_hash_next_index_insert (*removed, &entry, sizeof (Zval *), NULL); }} else/* Other cases, skip these elements */for (; Pos<offset+length && p; pos++, p=p->plistnext);/* If there are elements that need to be inserted, there is replaceme         NT parameter */if (list! = NULL) {/* For each element, create a new zval, copy and write it to the returned array */for (i=0; i<list_count; i++) {entry = *list[i];        entry->refcount++;    Zend_hash_next_index_insert (Out_hash, &entry, sizeof (Zval *), NULL);    }}/* Copy the remainder of the element */for (; p; p=p->plistnext) {entry = * ((Zval *) p->pdata);    entry->refcount++; if (p->nkeylength) zend_hash_quick_update (Out_hash, P->arkey, P->nkeylength, p->h, &entry, sizeof (Z    Val *), NULL); else Zend_hash_next_index_insert (Out_hash, &entry, sizeof (Zval *), NULL);} /* Resets the cursor of the array, equivalent to the Reset function */zend_hash_internal_pointer_reset (out_hash);

The whole process:
1. Copy elements from start to offset to return array
2. Remove elements from offset start to offset+length
3. If there is a replacement element, insert the new element into the position behind offset
4. Insert the function after offset+length into the return array
5. Resetting array cursors

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.