Deleting a single array element
Use unset() or, alternatively, array splice to remove just one element from an array ().
You can use array search() to find the key if you know the value but not the key to delete the element. Given that array search only gives the first hit, this only functions if the element does not appear more than once.
unset()
Keep in mind that the keys of the array won't change if you call unset(). Use array values() following unset() to reindex the keys. This will change all of the keys to numerically enumerated keys beginning with 0.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
// ↑ Key which you want to delete
Output:
[
[0] => a
[2] => c
]
Deleting multiple array elements
Depending on whether you know the values or the keys of the items you wish to delete, you can use the functions array diff() or array diff key() if you want to delete many array members without repeatedly calling unset() or array splice().
array diff() technique
Use "array diff" if you know the values of the array members you want to remove (). The keys of the array won't change, just like with unset() before it.
Code:
$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = \array_diff($array, ["a", "c"]);
// └────────┘
// Array values which you want to delete
Output:
[
[1] => b
]
I hope this helps you.