php remove an item from an array code example

Example 1: php delete element by value

$colors = array("blue","green","red");

//delete element in array by value "green"
if (($key = array_search("green", $colors)) !== false) {
    unset($colors[$key]);
}

Example 2: php remove item array

$items = ['banana', 'apple'];

unset($items[0]);

var_dump($items); // ['apple']

Example 3: php remove element from array

$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)

Example 4: php erase element from array

foreach ($items as $key =>$item){
  if(condition){
    unset($item[$key]);
  }
}

Example 5: how to delete item from array php

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);

Example 6: remove array values php

array_splice(array, start, length, array)

Tags:

Php Example