PHP removes code for specific elements in an array _php tutorial

Source: Internet
Author: User
For example, the following program:
Copy CodeThe code is as follows:

$arr = Array (' Apple ', ' banana ', ' cat ', ' dog ');

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

?>

Program Run Result:
Copy CodeThe code is as follows: Array ([0] = Apple [1] = banana [3] = = dog)
But the biggest drawback of this approach is that there is no rebuilding of the array index, which means that the third element of the array is gone.
After checking the data, the original PHP provided this function, but very indirect. This function is Array_splice ().
For ease of use, I encapsulated a function that makes it easy for you to use:
Copy CodeThe 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, it is known that the 2 location element is actually deleted and the index is re-established.
Program Run Result:
Copy CodeThe code is as follows:
Array ([0] = Apple [1] = banana [2] = = dog)

PHP Array_splice () function
The Array_splice () function, like the Array_slice () function, selects a series of elements in an array, but does not return, but instead deletes them and replaces them with other values. If the fourth argument is supplied, the previously selected elements will be replaced by the array specified by the fourth parameter.
The last generated array will be returned.
Syntax: Array_splice (Array,offset,length,array)
Array: Required. Specifies the array.
Offset: Required. Numerical. 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 the input array.
Length: Optional. Numerical. If this argument is omitted, all parts of the array from offset to end are moved. If length is specified and positive, then so many elements are removed. If length is specified with a negative value, all elements in the middle of offset to the end of the array ending with length are removed.
Array: The removed element is substituted by the elements in this array. If no value is removed, the elements in this array are inserted into the specified position.
If the function does not delete any elements (length=0), the alternate array is inserted from the position of the start parameter.
Example 1:
Copy CodeThe code is as follows:
$a 1=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");
$a 2=array (0=> "Tiger",1=> "Lion");
Array_splice ($a 1,0,2, $a 2);
Print_r ($a 1);
?>
Output:array ([0] = Tiger [1] = Lion [2] = = Horse [3] = Bird)

Example 2:
Copy CodeThe code is as follows:
$a 1=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");
$a 2=array (0=> "Tiger",1=> "Lion");
Print_r (Array_splice ($a 1,0,2, $a 2));
?>
Output:array ([0] = Dog [1] = Cat)

Example 3:
Copy CodeThe code is as follows:
The length parameter is set to 0
$a 1=array (0=> "Dog",1=> "Cat");
$a 2=array (0=> "Tiger",1=> "Lion");
Array_splice ($a 1,1,0, $a 2);
Print_r ($a 1);
?>
Output:array ([0] = Dog [1] = Tiger [2] = Lion [3] = Cat)

http://www.bkjia.com/PHPjc/325515.html www.bkjia.com true http://www.bkjia.com/PHPjc/325515.html techarticle For example, the following program: Copy code code as follows: PHP $arr = array (' Apple ', ' banana ', ' cat ', ' dog '); unset ($arr [2]); Print_r ($arr); program run Result: Copy code code such as The next ...

  • Related Article

    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.