223

Given an associative array:

array("key1" => "value1", "key2" => "value2", ...)

How would I go about removing a certain key-value pair, given the key?

user229044
  • 232,980
  • 40
  • 330
  • 338
gsquare567
  • 2,847
  • 3
  • 23
  • 21

7 Answers7

433

You can use unset:

unset($array['key-here']);

Example:

$array = array("key1" => "value1", "key2" => "value2");
print_r($array);

unset($array['key1']);
print_r($array);

unset($array['key2']);
print_r($array);

Output:

Array
(
    [key1] => value1
    [key2] => value2
)
Array
(
    [key2] => value2
)
Array
(
)
Pang
  • 9,564
  • 146
  • 81
  • 122
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 27
    +1: Thanks for the help. PHP newb here, but it's worth noting that if you are trying to perform these edits inside of a `foreach` loop, then you need to prepend an ampersand to your enumeration variable to allow write access. – FreeAsInBeer Jul 30 '12 at 21:20
  • 4
    Here is a link to a solution that illustrates the comment by @FreeAsInBeer [link](https://stackoverflow.com/a/7617643/9090980) with respect to the ampersand. – DanimalReks Jun 04 '19 at 21:52
  • Is this destructive of the original array? Is there a clean functional paradigm way to do this that produces an entirely new array without the value? – Urasquirrel Sep 10 '21 at 21:04
  • What is the best approach for case insensitivity (key1, Key1, KEY1 etc.)? – Vineet Sajwan Jun 27 '23 at 06:18
32

Use this function to remove specific arrays of keys without modifying the original array:

function array_except($array, $keys) {
  return array_diff_key($array, array_flip((array) $keys));   
} 

First param pass all array, second param set array of keys to remove.

For example:

$array = [
    'color' => 'red', 
    'age' => '130', 
    'fixed' => true
];
$output = array_except($array, ['color', 'fixed']);
// $output now contains ['age' => '130']
userlond
  • 3,632
  • 2
  • 36
  • 53
Bafi
  • 565
  • 5
  • 7
  • 1
    you need to close your quotes on `$output = array_except($array_1, ['color', 'fixed']);` – Abraham Brookes Jul 27 '16 at 07:05
  • plus one! functional! – Urasquirrel Sep 10 '21 at 21:06
  • I needed reverse functionality, is there in built functionality? ```php function array_extract($array, $keys) { return array_intersect_key($array, array_flip((array) $keys)); } $output = array_extract($array, ['color', 'fixed']); print_r($output); //Array ( [color] => red [fixed] => 1 ); ``` – Sahil Kashyap Dec 31 '21 at 08:47
27

Use unset():

unset($array['key1']);
Pang
  • 9,564
  • 146
  • 81
  • 122
cletus
  • 616,129
  • 168
  • 910
  • 942
10

Using unset:

unset($array['key1'])
user229044
  • 232,980
  • 40
  • 330
  • 338
Cristian
  • 198,401
  • 62
  • 356
  • 264
7

Consider this array:

$arr = array("key1" => "value1", "key2" => "value2", "key3" => "value3", "key4" => "value4");
  • To remove an element using the array key:

    // To unset an element from array using Key:
    unset($arr["key2"]);
    var_dump($arr);
    // output: array(3) { ["key1"]=> string(6) "value1" ["key3"]=> string(6) "value3" ["key4"]=> string(6) "value4" }
    
  • To remove element by value:

    // remove an element by value:
    $arr = array_diff($arr, ["value1"]);
    var_dump($arr);
    // output: array(2) { ["key3"]=> string(6) "value3" ["key4"]=> string(6) "value4" } 
    

read more about array_diff: http://php.net/manual/en/function.array-diff.php

  • To remove an element by using index:

    array_splice($arr, 1, 1);
    var_dump($arr);
    // array(1) { ["key3"]=> string(6) "value3" } 
    

read more about array_splice: http://php.net/manual/en/function.array-splice.php

Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34
4

You may need two or more loops depending on your array:

$arr[$key1][$key2][$key3]=$value1; // ....etc

foreach ($arr as $key1 => $values) {
  foreach ($values as $key2 => $value) {
  unset($arr[$key1][$key2]);
  }
}
Suraj Lulla
  • 489
  • 5
  • 12
Ruth VBA
  • 49
  • 1
3

you can do it using Laravel helpers:

first helper, method Arr::except:

$array = ['name' => 'Desk', 'price' => 100];

$filtered = Arr::except($array, ['price']);

// ['name' => 'Desk']

second helper: method Arr::pull

$array = ['name' => 'Desk', 'price' => 100];

$name = Arr::pull($array, 'name');

// $name: Desk

// $array: ['price' => 100]
OMR
  • 11,736
  • 5
  • 20
  • 35