Home > Backend Development > PHP Tutorial > PHP delete array element example sharing_PHP tutorial

PHP delete array element example sharing_PHP tutorial

WBOY
Release: 2016-07-13 10:25:06
Original
745 people have browsed it

Specific method of deleting array elements in PHP:

1. Use the unset() method:

Copy the code The code is as follows:

$a=array("red", "green", "blue", "yellow");
count($a); //Get 4
unset($a[1]); //Delete The second element
count($a); //Get 3
echo $a[2]; //There are only three elements in the array. I wanted to get the last element, but I got blue,
echo $a[1]; //No value
?>

Disadvantage: After deleting elements in the array, the number of elements in the array (obtained with count()) changes, but the array subscripts are not rearranged. You must also use PHP to delete the key before the array element to operate accordingly. value.

2. Use the array_splice() method:

Copy the code The code is as follows:

$a=array("red", "green", "blue", "yellow");
count ($a); //Get 4
array_splice($a,1,1); // Delete the second element
count ($a); //Get 3
echo $a[2]; //Get yellow
echo $a[1]; //Get blue
? >

Comparing this program with the previous one, you can see that array_splice() not only deletes the elements, but also rearranges the elements so that there will be no null values ​​among the elements of the array!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825228.htmlTechArticleThe specific method of deleting array elements in PHP: 1. Use the unset() method: Copy the code as follows: ?php $ a=array("red", "green", "blue", "yellow"); count($a); //Get 4 unset($a[1]); //Delete the...
Related labels:
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