| |
|
PHP deleting elements of an array by unset |
We can remove an element from an array by using unset command. However unset command is used to destroy any other variable and same way we can use delete any element of an array. This unset command takes the array key as input and removed that element from the array. After removal the associated key and value does not change. Let us see one simple example of this.
unset($input[3]);
Here we are removing the element with key=3. If the array has 7 elements and if try to delete 9th element then unset command will not return any error but noting will be deleted.
Here is an example how unset command is used.
$input=array(a,b,c,d,e,f,g);
unset($input[3]);
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
As we are deleting the third element the output of above code is here.
0 -> a 1 -> b 2 -> c 4 -> e 5 -> f 6 -> g
| |
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|
|
|