Answers for "php how to remove element from array"

PHP
12

php remove item array

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

unset($items[0]);

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

php delete element from array

//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
Posted by: Guest on August-06-2019
6

Deleting an element from an array in PHP

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]); //Key which you want to delete
/*
$array:
[
    [0] => a
    [2] => c
]
*/
//OR
$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);//Offset which you want to delet
/*
$array:
[
    [0] => a
    [1] => c
]
*/
Posted by: Guest on May-18-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
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
0

how to delete item from array php

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
Posted by: Guest on October-20-2020

Code answers related to "php how to remove element from array"

Browse Popular Code Answers by Language