Removing Elements from an Array

Using unset() Function:

PHP unset() removes a specified element from an array.

$array = ["element1", "element2", "element3"]; 
// Initialize an array
unset($array[1]);
// Remove the element at index 1 ("element2")

Using array_pop() Function:

PHP array_pop() removes and returns the last element of an array.

$array = ["element1", "element2", "element3"]; // Initialize an array
$lastElement = array_pop($array);
// Remove and return the last element ("element3")

Using array_shift() Function:

PHP array_shift() removes and returns the first element of an array, shifting all other elements to a lower index.

$array = ["element1", "element2", "element3"]; 
// Initialize an array
$firstElement = array_shift($array);
// Remove and return the first element ("element1")

How to add and remove Elements from Array in PHP?

Adding and removing elements from arrays in PHP can be achieved using various built-in functions and array manipulation techniques.

Similar Reads

Adding Elements to an Array:

Using [ ] Syntax:...

Removing Elements from an Array

Using unset() Function:...

Contact Us