PHP code for deleting specific elements in an array _ PHP Tutorial

Source: Internet
Author: User
PHP deletes the code of a specific element in the array. For example, the following program: Copy the code as follows :? Php $ arrarray (apple, banana, cat, dog); unset ($ arr [2]); print_r ($ arr );? Program running result: Copy the code as follows:

The code is as follows:



$ Arr = array ('apple', 'banana ', 'cat', 'dog ');

Unset ($ arr [2]);
Print_r ($ arr );

?>


Program running result:

The code is as follows:

Array ([0] => apple [1] => banana [3] => dog)


However, the biggest disadvantage of this method is that the array index is not rebuilt, that is, the third element of the array is gone.
After checking the information, PHP provided this function, but it was indirect. This function is array_splice ().
For ease of use, I encapsulated a function for your convenience:

The code is as follows:



Function array_remove (& $ arr, $ offset)
{
Array_splice ($ arr, $ offset, 1 );
}

$ Arr = array ('apple', 'banana ', 'cat', 'dog ');

Array_remove ($ arr, 2 );
Print_r ($ arr );
?>


After testing, we can know that the element 2 is actually deleted and the index is re-created.
Program running result:

The code is as follows:


Array ([0] => apple [1] => banana [2] => dog)


PHP array_splice () function
The array_splice () function is similar to the array_slice () function. it selects a series of elements in the array but does not return them. Instead, it deletes them and replaces them with other values. If the fourth parameter is provided, the selected elements are replaced by the array specified by the fourth parameter.
The final generated array will be returned.
Syntax: array_splice (array, offset, length, array)
Array: required. Specified array.
Offset: required. Value. If the offset value is positive, it is removed from the offset specified by this value in the input array. If offset is negative, it is removed from the offset specified by the reciprocal value at the end of the input array.
Length: Optional. Value. If this parameter is omitted, all parts from the offset to the end of the array are removed. If length is specified and it is a positive value, so many elements are removed. If length is specified and it is a negative value, all the elements from the offset to the reciprocal length at the end of the array are removed.
Array: the removed element is replaced by the element in the array. If no value is removed, the elements in this array are inserted to the specified position.
If the function does not delete any element (length = 0), the replacement array is inserted from the position of the start parameter.
Example 1:

The code is as follows:


$ A1 = array (0 => "Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird ");
$ A2 = array (0 => "Tiger", 1 => "Lion ");
Array_splice ($ a1, 0, 2, $ a2 );
Print_r ($ a1 );
?>
// Output: Array ([0] => Tiger [1] => Lion [2] => Horse [3] => Bird)


Example 2:

The code is as follows:


$ A1 = array (0 => "Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird ");
$ A2 = array (0 => "Tiger", 1 => "Lion ");
Print_r (array_splice ($ a1, 0, 2, $ a2 ));
?>
// Output: Array ([0] => Dog [1] => Cat)


Example 3:

The code is as follows:


// Set the length parameter to 0.
$ A1 = array (0 => "Dog", 1 => "Cat ");
$ A2 = array (0 => "Tiger", 1 => "Lion ");
Array_splice ($ a1, 1, 0, $ a2 );
Print_r ($ a1 );
?>
// Output: Array ([0] => Dog [1] => Tiger [2] => Lion [3] => Cat)

The pipeline code is as follows :? Php $ arr = array ('apple', 'banana ', 'cat', 'dog'); unset ($ arr [2]); print_r ($ arr );? Program running result: the code is as follows...

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.