Sometimes, we want to delete an element from an array in PHP.
In this article, we’ll look at how to delete an element from an array in PHP.
How to delete an element from an array in PHP?
To delete an element from an array in PHP, we can use the array_splice
method.
For instance, we write
$array = [0 => "a", 1 => "b", 2 => "c"];
\array_splice($array, 1, 1);
to call array_splice
on $array
with index 1 and delete 1 item as specify by the last 2 arguments.
Then $array
is
[
[0] => a
[1] => c
]
after we call array_splice
.
Conclusion
To delete an element from an array in PHP, we can use the array_splice
method.