Home > Backend Development > PHP Tutorial > PHP array introductory tutorial: delete elements from the end of the array

PHP array introductory tutorial: delete elements from the end of the array

WBOY
Release: 2016-07-25 08:58:01
Original
844 people have browsed it
This article introduces the method of deleting elements from the end of the array in PHP array operations. Friends in need can refer to it.

In php array function, array_pop() function deletes and returns the last element of the array.

The form is: mixed array_pop(aray target_array);

Example, removing the last state from the $states array:

<?php
$fruits = array("apple","banana","orange","pear");  
$fruit = array_pop($fruits);  
//$fruits = array("apple","banana","orange");  
//$fruit = "pear";
?>
Copy after login

The following is a detailed introduction to the methods and examples of deleting specific elements in an array in PHP.

Example 1,

<?php
//使用unset删除数组元素
$arr = array('apple','banana','cat','dog'); 

unset($arr[2]); 
print_r($arr); 
?> 
Copy after login

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

Disadvantage: The array index is not rebuilt, that is, the third element of the array is gone.

At this time, you can use the function array_splice(). The following is the encapsulation function implemented by myself:

<?php 
function array_remove(&$arr, $offset) 
{ 
array_splice($arr, $offset, 1); 
} 

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

array_remove($arr, 2); 
print_r($arr); 
?>
Copy after login
The element at position

2 is actually deleted and re-indexed. result: Array ( [0] => apple [1] => banana [2] => dog )

array_splice() function The array_splice() function is similar to the array_slice() function, selecting a range of elements in an array, but instead of returning them, it deletes them and replaces them 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 final 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 at the position of the start argument.

Example:

<?php 
//--例1

$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 ) 

//--例2

$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 ) 

//--例3
// length 参数设置为 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 )
?>
Copy after login


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