In this tutorial, you shall learn how to delete an element at specific index in given array in PHP using unset() function, with the help of example programs.

PHP – Delete element at specific index in array

To delete an element at specific index in given array in PHP, we can use unset() function.

The syntax to remove an element at index index from an array arr using unset() function is

</>
Copy
unset($arr[$index])

Examples

1. Delete element from array, at index=2

In this example, we will take an array with five elements, and delete the element at index=2 from the array.

PHP Program

</>
Copy
<?php
  $arr = array("apple", "banana", "mango", "cherry", "guava");
  $index = 2;
  unset($arr[$index]);
  print_r($arr);
?>

Output

PHP - Delete element at specific index in array

Conclusion

In this PHP Tutorial, we learned how to delete an element at specific index in an array, using PHP Array unset() function.