234

Given an associative array:

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

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

0

7 Answers 7

444

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
(
)
Sign up to request clarification or add additional context in comments.

4 Comments

+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.
Here is a link to a solution that illustrates the comment by @FreeAsInBeer link with respect to the ampersand.
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?
What is the best approach for case insensitivity (key1, Key1, KEY1 etc.)?
36

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']

3 Comments

you need to close your quotes on $output = array_except($array_1, ['color', 'fixed']);
plus one! functional!
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 );
27

Use unset():

unset($array['key1']);

Comments

10

Using unset:

unset($array['key1'])

Comments

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

Comments

4

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]

Comments

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]);
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.