For learningI began by referring to other methods, using unset, but with a flaw. A $ A is an array:
- $ 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, and I wanted to get the last element, but I got blue,
- echo $a [1]; No value
- ?>
That is, after the elements in the array are deleted, the number of elements in the array (with count () is changed, but the array subscript is not rearranged, and the corresponding value must be manipulated by removing the key from the array element before PHP.
Later I used another method, in fact, is not called "method", is to use PHP4 ready-made function Array_splice ().
- !--? $ a = array ("Red", "green", "blue", "yellow");
- count ($a);//Get 4
- array_splice ($a, 1, 1);//delete second element
- count ($a);//Get 3
- echo $a [2];//Get yellow
- echo $a [1];//Get blue
- ;
Comparing this program to the previous one, you can see that array_splice () not only removes the element, but also rearrange the elements so that there will be no null values in the middle of each element of the array (as in the preceding example, $a[1]).
Array_splice () is actually a function of replacing an array element, but simply PHP removes the array element without replacing the value. The following is the use of Array_splice ():
The parameter input is an array to manipulate; offset is the beginning of the first element, the number of seconds from the beginning, the number from the last element, and the length of the number of elements to be replaced/removed, and when omitted, from offset to the end of the array, can be positively negative, The principle is the same as offset; relacement is the value to be replaced.
This php delete array element function is already the standard function of PHP4, but in my hands of the PHP4 manual did not mention, I was downloaded php.net the latest manual found. I don't know, a look scare, I hand of the PHP4GB (believe is also the hands of most people) It's too old to be a lot of functions. To know that PHP is well-known as a function, if we do not know a lot of functions, and how to put the domestic programming level up? I wish I could have a bunch of PHP keen who can translate the latest PHP manuals again. I'm at the ADE Technology Center.
http://www.bkjia.com/phpjc/446338. HTML www.bkjia.com true http://www.bkjia.com/phpjc/446338.html techarticle for learning to start I refer to other methods, using unset, but there is a flaw. $a is an array:? $ A = Array ("Red", "green", "blue", "yellow"); Count ($a);//Get 4 unset ($a [1]);//delete section ...