PHP Usuń element z tablicy
if (in_array('strawberry', $array))
{
unset($array[array_search('strawberry',$array)]);
}
Weary Wildebeest
if (in_array('strawberry', $array))
{
unset($array[array_search('strawberry',$array)]);
}
$items = ['banana', 'apple'];
unset($items[0]);
var_dump($items); // ['apple']
$arr = array('a' => 1, 'b' => 2, 'c' => 3);
unset($arr['b']);
// RESULT: array('a' => 1, 'c' => 3)
$arr = array(1, 2, 3);
array_splice($arr, 1, 1);
// RESULT: array(0 => 1, 1 => 3)
//Delete array items with unset(no re-index) or array_splice(re-index)
$colors = array("red","blue","green");
unset($colors[1]);//remove second element, do not re-index array
$colors = array("red","blue","green");
array_splice($colors, 1, 1); //remove second element, re-index array
$arr1 = array(
'geeks', // [0]
'for', // [1]
'geeks' // [2]
);
// remove item at index 1 which is 'for'
unset($arr1[1]);
// Re-index the array elements
$arr2 = array_values($arr1);
// Print re-indexed array
var_dump($arr1);
unset($user[$i]);
// Re-index the array elements
$user = array_values($user);