Home > Article > Backend Development > Introduction to specific functions of PHP to delete array elements_PHP tutorial
When we write actual code, we often deal with arrays, which is difficult for beginners. Today we will introduce to you how to delete array elements in PHP.
It is very simple to add elements to an array in PHP. Just use assignment directly. The key of the array will be automatically increased, but what about deleting elements in the array? Have you ever thought about it? Is it rarely encountered? I recently encountered the problem of deleting array elements in PHP when working on a shopping basket program. After searching for a long time, I finally found a way to delete arrays. It is actually very simple.
At the beginning, I referred to an article "String Array, The method in "Delete Array Elements" (available in OSO) uses unset, but there is a flaw. For example, $a is an array:
<ol class="dp-xml"> <li class="alt"><span><span class="tag">< ?</span><span> $</span><span class="attribute">a</span><span>=</span><span class="attribute-value">array</span><span>("red", "green", "blue", "yellow"); </span></span></li><li><span>count($a); //得到4 </span></li><li class="alt"><span>unset($a[1]); //删除第二个元素 </span></li><li><span>count($a); //得到3 </span></li><li class="alt"><span>echo $a[2]; //数组中仅有三个元素,本想得到最后一个元素,但却得到blue, </span></li><li><span>echo $a[1]; //无值 </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li> <li><span> </span></li> </ol>
In other words, delete the array in PHP After the element, the number of elements in the array (obtained using count()) changed, but the array subscripts were not rearranged, and the corresponding value must be operated by deleting the key before the array.
Later I used another method This method is actually not called a "method" at all. It uses the ready-made function array_splice() in PHP4.
<ol class="dp-xml"> <li class="alt"><span><span class="tag">< ?</span><span> $</span><span class="attribute">a</span><span>=</span><span class="attribute-value">array</span><span>("red", "green", "blue", "yellow"); </span></span></li><li><span>count ($a); //得到4 </span></li><li class="alt"><span>array_splice($a,1,1); //删除第二个元素 </span></li><li><span>count ($a); //得到3 </span></li><li class="alt"><span>echo $a[2]; //得到yellow </span></li><li><span>echo $a[1]; //得到blue </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li> <li><span> </span></li> </ol>
Comparing this program with the previous one, you can see ,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 (such as $a[1] in the previous example).
array_splice() actually It is a function that replaces array elements, but if no replacement value is added, the element is simply deleted. The following is the usage of array_splice():
array array_splice (array input, int offset [, int length [, array replacement]])
The parameter input is the array to be operated on; offset is the starting element, if it is positive, it starts counting from the first element, if it is negative, it starts counting from the last element; length is the number to be replaced/PHP deleted The number of array elements. When omitted, it starts 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.