PHP removes the specified element from the array and replaces the function with other values Array_splice ()

Source: Internet
Author: User

Instance

Remove the element from the array and replace it with a new element:

<?php$a1=array ("A" = "red", "b" = "green", "c" = "Blue", "D" and "yellow"), $a 2=array ("a" and "Purple", "B "=" orange "); Array_splice ($a 1,0,2, $a 2);p Rint_r ($a 1);? >

Definition and usage

The Array_splice () function removes the selected element from the array and replaces it with a new element. The function also returns an array of the elements that were removed.

Tip: If the function does not remove any elements (length=0), the alternate array is inserted from the position of the start parameter (see Example 2).

Note: The key names in the alternate array are not preserved.

Grammar

Array_splice (Array,start,length,array)
Parameters Describe
Array Necessary. Specifies the array.
Start Necessary. Numerical. Specifies the start position of the delete element. 0 = first element. If the value is set to a positive number, the offset specified by the value in the array begins to be removed. If the value is set to a negative number, it is removed from the offset specified by the value of the end of the array. 2 means starting with the second-to-last element of the array.
Length Optional. Numerical. Specifies the number of elements to be removed, and also the length of the returned array. If the value is set to a positive number, the element is removed. If the value is set to a negative number, all elements in the middle of the countdown from start to the end of the array are removed. If the value is not set, remove all elements starting at the position set by the start parameter until the end of the array.
Array Optional. Specifies an array with elements to insert in the original array. If there is only one element, you can set it to a string and do not need to set an array.

Technical details

return value: Returns an array containing the extracted elements.
PHP version: 4 +

More examples

Example 1

Same as the previous part of this page, but the output returns an array:

<?php$a1=array ("A" = "red", "b" = "green", "c" = "Blue", "D" and "yellow"), $a 2=array ("a" and "Purple", "B "=" Orange ");p Rint_r (Array_splice ($a 1,0,2, $a 2));? >

Example 2

With the length parameter set to 0:

<?php$a1=array ("0" = "Red", "1" = "green"), $a 2=array ("0" = "Purple", "1" and "Orange") Array_splice ($a 1 , 1,0, $a 2);p Rint_r ($a 1);? >

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 elem Ents */if (return_value_used) {//if useful to function return value create return array, otherwise do not create return array   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); }

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.

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.