php

How to Delete an Element from an Array PHP?

In PHP programming, working with arrays is a fundamental task that involves various operations, such as adding, modifying, and deleting elements. Deleting an element from an array is a crucial operation that allows you to manage your data more efficiently. Whether you want to remove a specific element or clear the entire array, PHP provides several methods to accomplish this task.

This article will examine various methods for removing an element from an array in PHP, along with examples and explanations.

Delete an Element from an Array PHP

Deleting an element from an array in PHP can be done through the following functions:

1: Using unset() Function

One of the most commonly used methods for deleting an element from an array in PHP is to use the built-in unset() function that removes an element from an array once you specify its index. When you call the unset() function in a PHP code, the value of the element is set to NULL, and the index is removed from the array. It is possible to use the unset() function in PHP to eliminate elements from arrays, regardless of whether they are numeric or associative arrays.

The following example uses the unset() function in PHP:

<?php

$color = array("Yellow", "Black", "Brown", "White");

echo "Original array: ";

print_r($color);

unset($color[2]);

echo "Modified array: ";

print_r($color);

?>

In the above program, we have deleted the element from an array at index 2, which is “Brown”.

Output

Note that the unset() function is not re-indexing the array.

2: Using array_splice() Function

Another way to remove an element from an array in PHP is to use the array_splice() function, which is capable of removing one or more elements from an array and can also replace them with new elements if necessary.

The simple syntax for array_splice() function is as follows:

array_splice(array &$array, int $start, int $length = 0, mixed $replacement = array()): array

Where $array is the input array that needs to be modified, $start is the index from where the elements need to be removed or added. The $length is the number of elements to remove from the array, starting from $start, which does not remove the element if it’s 0. $replacement refers to the array or values that will replace the elements removed from the original array. It is an optional parameter and can be used to replace the removed elements with new values or insert new values at the specified index.

The below-given example illustrates the use of the array_splice() function:

<?php

$color = array("Yellow", "Black", "Brown", "White");

echo "Original array: ";

print_r($color);

array_splice($color, 2,1);

echo "Modified array: ";

print_r($color);

?>

Output

Note that the array_splice() function is also re-indexing the array.

3: Using array_filter Function

The array_filter() function is another method for deleting an element from an array. It operates by iterating through the array elements and producing a new array that only contains the elements meeting a specified callback function. By implementing a callback function that omits the element to be deleted, the array_filter() function can remove the element from the array.

The below-given example uses the array_filter() function:

<?php

$color = array("Yellow", "Black", "Brown", "White");

echo "Original array: ";

print_r($color);

$color = array_filter($color, function($value) {

    return $value !== "Brown";

});

echo "Modified array: ";

print_r($color);

?>

Output


The array_filter() is not re-indexing the array.

4: Using array_diff() Function

PHP offers another method to remove an element from an array, which is the array_diff() function that takes multiple arrays as input and returns the values that exist solely in the first array and are not present in any of the other arrays.

The following example uses the array_diff() function:

<?php

$color = array("Yellow", "Black", "Brown", "White");

echo "Original array: ";

print_r($color);

$color = array_diff($color, array("Brown"));

echo "Modified array: ";

print_r($color);

?>

Output

The array_diff() function is also not re-indexing the array.

Conclusion

Deleting an element from an array is an essential operation in PHP programming, and there are different methods to accomplish this task, including unset(), array_splice(), array_filter(), and array_diff() functions. By understanding these techniques, you can manage your arrays efficiently and optimize your PHP code.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.