how to remove a key and value pair from a array in php if condition is false 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 array remove keys keep values

<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>
Array
(
    [0] => XL
    [1] => gold
)

Tags:

Php Example