The array in PHP to add elements is very simple, directly with the assignment of the line, the array of key will automatically increase, but to delete the elements in the array? Have you ever thought about it? I recently encountered a shopping basket program to remove the elements in the array of problems, looking for half a day, Finally found the way to delete the array, in fact, very simple.
I began by referring to an article, "string array, delete array elements" (OSO) in the method, with unset, but there is a flaw. A $ A is an array:
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 with the key before the array is deleted.
Later I used another method, in fact, is not called "method", is to use PHP4 ready-made function Array_splice ().
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 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 it is simple to delete an element without replacing it. The following is the use of Array_splice ():
Array array_splice (array input, int offset [, int length [, array replacement]])
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 function is already a standard function of the PHP4, but in my hand PHP4 manual did not mention, I was the download php.net the latest manual found. I don't see, I see a fright, the PHP4GB in my hand (believed to be the hands of most people) is too old, Many functions are not available. To know that PHP is well-known as a function, if we do not know a lot of functions, 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:// tech.addcn.com/See the webmaster has a "Classic PHP Handbook" program, but it seems that there are not many, if you are interested readers can join.
http://www.bkjia.com/PHPjc/532079.html www.bkjia.com true http://www.bkjia.com/PHPjc/532079.html techarticle the array in PHP to add elements is very simple, directly with the assignment of the line, the array key will automatically increase, but to delete the elements in the array? Have you ever thought about it? I was in the office recently .