PHP code to delete specific elements in an array_PHP tutorial

WBOY
Release: 2016-07-21 15:18:29
Original
715 people have browsed it

For example, the following program:

Copy code The code is as follows:


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

unset($arr[2]);
print_r($arr);

? >

Program running result:
Copy code The code is as follows:
Array ( [0] = > apple [1] => banana [3] => dog )

But 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, it turns out that PHP provides this function, but it is very indirect. This function is array_splice().
For ease of use, I encapsulated it into a function for everyone to use:
Copy the code 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 at position 2 has been truly deleted and the index has been re-established.
Program running results:
Copy code The code is as follows:

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

PHP array_splice() function
array_splice() function is similar to array_slice() function, selecting a series of elements in the array, but Instead of returning them, they are deleted and replaced with other values. If a fourth argument is provided, those previously selected elements will be replaced by the array specified by the fourth argument.
The last generated array will be returned.
Syntax: array_splice(array,offset,length,array)
array: required. Specifies an array.
offset: required. numerical value. If offset is positive, removal begins at the offset specified by this value in the input array. If offset is negative, removal begins at the offset specified by this value from the end of the input array.
length: optional. numerical value. If this parameter is omitted, all parts of the array from offset to the end are removed. If length is specified and is positive, this many elements are removed. If length is specified and is negative, all elements from offset to length counting down from the end of the array are removed.
array: The removed elements are replaced by elements in this array. If no values ​​are removed, the element in this array is inserted at the specified position.
If the function removes no elements (length=0), the replacement array will be inserted from the position of the start argument.
Example 1:
Copy code 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:
Copy code 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:
Copy code The code is as follows:

// The length parameter is set 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 )

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325515.htmlTechArticleFor example, the following program: Copy the code as follows: ?php $arr = array('apple','banana' ,'cat','dog'); unset($arr[2]); print_r($arr); ? Program running results: Copy the code as follows...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template