Delete single or multiple element from array PHP

Delete single or multiple element from array PHP

In PHP, you can delete single or multiple elements from an array using different methods. Here are two commonly used approaches:

If you want to delete single element from array

$array = [0 => "Hello", 1 => "you", 2 => "beautiful"];
unset($array[1]); // Set the key you want to remove
OUTPUT :

[
    [0] => Hello
    [2] => beautiful
]

If you want to delete multiple element from array

// By value 
$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = \array_diff($array, ["a", "c"]);
OUTPUT :
[
    [1] => b
]

Refer array_diff for more info on function.