How can I remove a key and its value from an associative array?

PhpArrays

Php Problem Overview


Given an associative array:

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

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

Php Solutions


Solution 1 - Php

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
(
)

Solution 2 - Php

Use unset():

unset($array['key1']);

Solution 3 - Php

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

Solution 4 - Php

Using unset:

unset($array['key1'])

Solution 5 - Php

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

Solution 6 - Php

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

Solution 7 - Php

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]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongsquare567View Question on Stackoverflow
Solution 1 - PhpSarfrazView Answer on Stackoverflow
Solution 2 - PhpcletusView Answer on Stackoverflow
Solution 3 - PhpBafiView Answer on Stackoverflow
Solution 4 - PhpCristianView Answer on Stackoverflow
Solution 5 - PhpSahith VibudhiView Answer on Stackoverflow
Solution 6 - PhpRuth VBAView Answer on Stackoverflow
Solution 7 - PhpOMRView Answer on Stackoverflow