There are different ways to delete an array element.
Syntax:
Parameters:-
mixed $var - It takes variable as a parameter which can be anything e.g., $var, $arr[$key]. We can provide as many variables as possible.
So, let's understand the use of unset() using an example-
// output
Notice: Undefined variable: x in sample.php on line 12
So, this will remove that variable from the memory location and will free the memory. Now, we can use the same unset() function to destroy
array element as well. We have to provide a specific element which we want to delete. But, in this case, it will loose the indexing of an array.
Let's see the working example-
Array
(
[0] => 1
[1] => 2
[3] => 4
)
Here, as you can see the in the unset() function we passed second array key to be deleted. Hence, it deleted second element itself without reordering
the array element.
So, to reorder the array element we need to use array_values(), which takes an array as a prameter and return all the values of the array (which eventually reorders that array)
Applying on variable will give a warning as -
Warning: array_slice() expects parameter 1 to be array, string given...
Syntax-
Parameters-
array $input - an input array. & is added to make sure that the original array will be considered and the element which we want to delete should
also get destroyed from $input globally.
int $offset - the index position from which we want to delete the array elements. It can be +ve OR -ve.
int $length - It is the number of elements that will be removed.
mixed $replacement - If this parameter is defined then the removed elements are replaced with elements from this array.
So, let's understand the use of array_splice() using an example-
Example 1 :-
Array
(
[0] => washington
)
In this case, we removed all the elements from the index 1.
Example 2 :-
Array
(
[0] => washington
[1] => LA
[2] => Houston
)
In this case, we removed second element from the array because we started with index 1 (second paramenter) and then number of element that should
be removed from the array is 1 (third parameter).
Example 3 :-
Array
(
[0] => washington
[1] => San Francisco
[2] => LA
[3] => Houston
)
In this case, we have provided the third parameter, which will fill the gap created by second element.
//output
Array
(
[0] => washington
[1] => San Francisco
[2] => Boston
[3] => LA
[4] => Houston
)
In this case, we've provided third parameter as an array and this array element will be inserted into the gap created by second element deleted already.
1. Using unset() function -
To delete an element from an array, PHP has provided an inbuilt function (unset()) to destroy it.Syntax:
unset ( mixed $var [, mixed $... ] ) : void
Parameters:-
mixed $var - It takes variable as a parameter which can be anything e.g., $var, $arr[$key]. We can provide as many variables as possible.
So, let's understand the use of unset() using an example-
<?php $name = 'Mogan Fox'; // destroy variable unset($name); echo $name;
// output
Notice: Undefined variable: x in sample.php on line 12
So, this will remove that variable from the memory location and will free the memory. Now, we can use the same unset() function to destroy
array element as well. We have to provide a specific element which we want to delete. But, in this case, it will loose the indexing of an array.
Let's see the working example-
<?php $arr = [1,2,3,4]; unset($arr[2]); print_r($arr);// output
Array
(
[0] => 1
[1] => 2
[3] => 4
)
Here, as you can see the in the unset() function we passed second array key to be deleted. Hence, it deleted second element itself without reordering
the array element.
So, to reorder the array element we need to use array_values(), which takes an array as a prameter and return all the values of the array (which eventually reorders that array)
2. using array_slice()
This is different approach to delete an element from the array. As the name suggests this function is specifically used on array itself.Applying on variable will give a warning as -
Warning: array_slice() expects parameter 1 to be array, string given...
Syntax-
array_splice ( array &$input , int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) : array
Parameters-
array $input - an input array. & is added to make sure that the original array will be considered and the element which we want to delete should
also get destroyed from $input globally.
int $offset - the index position from which we want to delete the array elements. It can be +ve OR -ve.
int $length - It is the number of elements that will be removed.
mixed $replacement - If this parameter is defined then the removed elements are replaced with elements from this array.
So, let's understand the use of array_splice() using an example-
Example 1 :-
<?php $arr = ["washington", "NY", "LA", "Houston"]; array_splice($arr, 1); print_r($arr);//output
Array
(
[0] => washington
)
In this case, we removed all the elements from the index 1.
Example 2 :-
<?php $arr = ["red", "green", "blue", "yellow"]; array_splice($arr, 1, 1); print_r($arr);// output
Array
(
[0] => washington
[1] => LA
[2] => Houston
)
In this case, we removed second element from the array because we started with index 1 (second paramenter) and then number of element that should
be removed from the array is 1 (third parameter).
Example 3 :-
<?php $arr = ["washington", "NY", "LA", "Houston"]; array_splice($arr, 1, 1, "San Francisco"); print_r($arr);//output
Array
(
[0] => washington
[1] => San Francisco
[2] => LA
[3] => Houston
)
In this case, we have provided the third parameter, which will fill the gap created by second element.
<?php $arr = ["washington", "NY", "LA", "Houston"]; array_splice($arr, 1, 1, array("San Francisco", "Boston")); print_r($arr);
//output
Array
(
[0] => washington
[1] => San Francisco
[2] => Boston
[3] => LA
[4] => Houston
)
In this case, we've provided third parameter as an array and this array element will be inserted into the gap created by second element deleted already.
No comments:
Post a Comment