Answers for "php unset from array"

PHP
17

php delete array item by value

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

//delete element in array by value "green"
if (($key = array_search("green", $colors)) !== false) {
    unset($colors[$key]);
}
Posted by: Guest on October-30-2019
13

php remove item array

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

unset($items[0]);

var_dump($items); // ['apple']
Posted by: Guest on March-20-2020
5

php unset array key

unset($dataArray['key']);
Posted by: Guest on June-01-2020
1

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)
Posted by: Guest on March-04-2020
3

php erase element from array

foreach ($items as $key =>$item){
  if(condition){
    unset($item[$key]);
  }
}
Posted by: Guest on July-08-2020
0

remove item in an array php

//NO KEY supplied
$message array("a", "b", "c", "d");
$del_val = "b";
if (($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}
Posted by: Guest on June-10-2021

Browse Popular Code Answers by Language