Home > Backend Development > PHP Tutorial > Discuss how to delete array elements in PHP_PHP Tutorial

Discuss how to delete array elements in PHP_PHP Tutorial

WBOY
Release: 2016-07-15 13:29:48
Original
1172 people have browsed it

For learning , I followed other methods and used unset, but there was a flaw. For example, $a is an array:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>a</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>("red", "green", "blue", "yellow");   </SPAN></SPAN><LI class=""><SPAN>count($a); //得到4   </SPAN><LI class=alt><SPAN>unset($a[1]); //删除第二个元素   </SPAN><LI class=""><SPAN>count($a); //得到3   </SPAN><LI class=alt><SPAN>echo $a[2]; //数组中仅有三个元素,本想得到最后一个元素,但却得到blue,   </SPAN><LI class=""><SPAN>echo $a[1]; //无值   </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span>   </span></span></li></ol>
Copy after login

That is to say, after deleting the elements in the array, The number of elements in the array (obtained using count()) changed, but the array subscripts were not rearranged, and PHP had to be used to delete the keys in front of the array elements to operate the corresponding values.

Later I used another method Method, in fact, is not called "method" at all, it uses PHP4's ready-made function array_splice().

<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>a</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>("red", "green", "blue", "yellow");   </SPAN></SPAN><LI class=""><SPAN>count ($a); //得到4   </SPAN><LI class=alt><SPAN>array_splice($a,1,1); //删除第二个元素   </SPAN><LI class=""><SPAN>count ($a); //得到3   </SPAN><LI class=alt><SPAN>echo $a[2]; //得到yellow   </SPAN><LI class=""><SPAN>echo $a[1]; //得到blue   </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span>   </span></span></li></ol>
Copy after login

Comparing this program with the previous one, you can see that array_splice() not only deletes elements, and also rearranged 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() is actually a function that replaces array elements, but if Simple PHP deletes array elements without replacing values. The following is the usage of array_splice():

<ol class="dp-xml"><li class="alt"><span><span>array array_splice (array input, int offset <br>[, int length [, array replacement]])  </span></span></li></ol>
Copy after login

The parameter input is the array to be operated on; offset is the number from which Start with the element. If it is positive, count from the first element. If it is negative, count from the last element. Length is the number of elements to be replaced/delete. If it is omitted, it will start 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 PHP function to delete array elements is already a standard function of PHP4, but it is not mentioned in the PHP4 manual in my hand. I downloaded PHP I found it in the latest manual of .net. I didn’t know it, but 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, and many functions are missing. You must know that PHP is It 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 again. I am in "Ed" Technology Center"


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446338.htmlTechArticleFor the beginning of learning, I followed other methods, using unset, but there is a flaw. For example, $a is an array: ? $ a = array ("red","green","blue","yellow"); count($a);//Get 4 unset($a[1]);//Delete the first...
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