Remove keys from associative array php

Given an associative array:

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

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

user229044

226k40 gold badges323 silver badges333 bronze badges

asked Jun 16, 2010 at 13:14

0

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

Remove keys from associative array php

Pang

9,213146 gold badges85 silver badges118 bronze badges

answered Jun 16, 2010 at 13:15

Remove keys from associative array php

SarfrazSarfraz

371k74 gold badges529 silver badges575 bronze badges

3

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,4942 gold badges32 silver badges50 bronze badges

answered Sep 1, 2015 at 10:56

Remove keys from associative array php

BafiBafi

5164 silver badges7 bronze badges

3

Use unset():

unset($array['key1']);

Remove keys from associative array php

Pang

9,213146 gold badges85 silver badges118 bronze badges

answered Jun 16, 2010 at 13:15

cletuscletus

605k163 gold badges903 silver badges940 bronze badges

0

Using unset:

unset($array['key1'])

user229044

226k40 gold badges323 silver badges333 bronze badges

answered Jun 16, 2010 at 13:15

CristianCristian

197k62 gold badges355 silver badges262 bronze badges

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

answered Apr 19, 2018 at 14:32

Sahith VibudhiSahith Vibudhi

4,4932 gold badges27 silver badges30 bronze badges

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

Remove keys from associative array php

answered Mar 6, 2015 at 5:28

Remove keys from associative array php

0

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]

answered Mar 13, 2021 at 9:42

Remove keys from associative array php

OMROMR

10.9k5 gold badges13 silver badges30 bronze badges

How to remove particular key from associative array in PHP?

Method 1: Using unset() function: The unset() function is used to unset a key and its value in an associative array. print_r( $arr ); ?> Method 2: Using array_diff_key() function: This function is used to get the difference between one or more arrays.

How to remove keys from PHP array?

Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array.

How do you remove an element from an associative array?

We can remove objects from JavaScript associative array using delete keyword. Approach: Declare an associative array containing key-value pair objects. Then use delete keyword to delete the array objects from an associative array.

How to remove values from array in PHP?

In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically. Function Used: unset(): This function unsets a given variable.