The ‘unset’ function can be used to remove an element from the array and use the ‘array_values’ function that resets the indices of the array.
Example
<?php $my_arr = array( 'this', 'is', 'a', 'sample', 'only'); echo"The array is "; var_dump($my_arr); unset($my_arr[4]); echo"The array is now "; $my_arr_2 = array_values($my_arr); var_dump($my_arr_2); ?>
Output
The array is array(5) {
[0]=>
string(4) "this"
[1]=>
string(2) "is"
[2]=>
string(1) "a"
[3]=>
string(6) "sample"
[4]=>
string(4) "only"
}
The array is now array(4) {
[0]=>
string(4) "this"
[1]=>
string(2) "is"
[2]=>
string(1) "a"
[3]=>
string(6) "sample"
}An array is declared that contains string values. The array is displayed and the ‘unset’ function is used to delete a specific index element from the array. Then the array is displayed again to reflect the changes on the console.