213

Given an associative array:

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

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

user229044
  • 222,134
  • 40
  • 319
  • 330
gsquare567
  • 2,727
  • 3
  • 22
  • 21

7 Answers7

419

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
(
)
user229044
  • 222,134
  • 40
  • 319
  • 330
Sarfraz
  • 367,681
  • 72
  • 526
  • 573
  • 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
  • 3
    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
  • Upvote for a nice explanation with example. – Prabhu Nandan Kumar Feb 03 '22 at 07:20
27

Use unset():

unset($array['key1']);
cletus
  • 599,013
  • 161
  • 897
  • 938
27

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,456
  • 2
  • 31
  • 49
Bafi
  • 496
  • 4
  • 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
10

Using unset:

unset($array['key1'])
user229044
  • 222,134
  • 40
  • 319
  • 330
Cristian
  • 196,304
  • 62
  • 355
  • 262
6

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,315
  • 2
  • 27
  • 30
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
  • 370
  • 1
  • 7
Ruth VBA
  • 49
  • 1
1

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
  • 10,491
  • 5
  • 12
  • 29