Home > Backend Development > PHP Tutorial > Delete Array Elements_PHP Tutorial

Delete Array Elements_PHP Tutorial

WBOY
Release: 2016-07-13 17:25:19
Original
761 people have browsed it

It is very simple to add elements to an array in PHP. Just use assignment. The key of the array will be automatically added, but what about deleting elements in the array? Have you ever thought about it? Is it rare? I am dealing with a shopping trip recently. I encountered the problem of deleting elements in an array when I was using a basket program. After searching for a long time, I finally found a way to delete the array. It is actually very simple.
At first, I referred to an article "String Array, Deleting Array Elements" (OSO There is a method in ), using unset, but there is a flaw. For example, $a is an array:
$a=array("red", "green", "blue", "yellow"); $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
?>
That is to say, deletion After the elements in the array, the number of elements in the array (obtained using count()) changed, but the array subscripts were not rearranged, and the corresponding values ​​must be operated by deleting the keys before the array.
Later I Another method is used, which is actually not called a "method" at all. It uses the ready-made function array_splice() in PHP4.
$a=array("red", "green", "blue", "yellow" ); $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
?>
Compare 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 are no null values ​​among the elements of the array (such as $a[1] in the previous example).
array_splice() is actually a function that replaces array elements. , but simply delete the element without adding a replacement value. The following is the usage of array_splice():
array array_splice (array input, int offset [, int length [, array replacement]])


parameters input is the array to be operated on; offset is the starting element, counting from the first element when it is positive, and counting from the last element when it is negative; length is the number of elements to be replaced/delete, when omitted From offset to the end of the array, it can be positive or negative. The principle is the same as offset; replacement is the value to be replaced.
This function is already a standard function of PHP4, but it is mentioned in the PHP4 manual in my hand. I didn’t mention it. I found it after downloading the latest manual of php.net. I didn’t know it until I saw it. I was shocked when I saw it. The PHP4gb in my hand (I believe it is also in the hands of most people) is too old. There are many There are no functions. You must know that PHP is famous for its complete functions. If we don’t even know many functions, how can we improve the domestic programming level? I really hope that a group of PHP enthusiasts can translate the latest PHP manual. I saw in the "Add Technology Center" (http://tech.addcn.com/) that the webmaster has a "Classic PHP Manual" plan, but there don't seem to be many applicants. Readers who are interested can join. Among them.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532079.htmlTechArticleIt is very simple to add elements to an array in PHP. Just use assignment directly. The key of the array will automatically increase, but Want to delete elements in an array? Have you ever thought about it? Is it rare? I have been everywhere recently...
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