Delete an item from an array without a loop in PHP

I recently bumped into a nice way of removing an item from an array without using a loop when you don’t know the key of the item.

$items = [1, 2, 3, 4];
$itemToDelete = 3;
$itemsWithout3 = array_values(array_diff($items, [$itemToDelete]));
//$itemsWithout3 => [1, 2, 4]

We set the item to delete into a new array and take the difference of the actual array using array_diff. The difference is of course the array without the item we want to delete. array_diff leaves a gap in the array indexes if the value we delete is not in the end of the array,  in the example we would have indexes 0, 1, 3 after running array_diff. To reset the indexes, we use array_values to create a new array with the values we want.

I found this to be a nice little trick that will save a loop when you want a simple deletion.