如何从关联数组中删除键及其值?

时间:2022-08-26 14:11:45

Given an associative array:

给定一个关联数组:

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

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

如果给定键值,我该如何删除某个键值对?

7 个解决方案

#1


276  

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

#2


21  

Use unset():

使用设置():

unset($array['key1']);

#3


13  

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

#4


4  

Using unset:

使用设置:

unset($array['key1'])

#5


1  

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

根据您的数组,您可能需要两个或多个循环:

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

foreach ($arr as $key1 => $values) {
  foreach ($key1 as $key2 => $value) {
  unset($arr[$key1][$key2]);
  }
}

#6


0  

Below is a method that removes items from an associative with offset, length and replacement - using array_splice

下面是一个方法,使用array_splice从关联式中删除带有偏移量、长度和替换的项

    function array_splice_assoc(&$input, $offset, $length = 1, $replacement = []) {
        $replacement = (array) $replacement;
        $key_indices = array_flip(array_keys($input));
        if (isset($input[$offset]) && is_string($offset)) {
           $offset = $key_indices[$offset];
        }
        if (isset($input[$length]) && is_string($length)) {
            $length = $key_indices[$length] - $offset;
        }

        $input = array_slice($input, 0, $offset, TRUE) + $replacement + array_slice($input, $offset + $length, NULL, TRUE);
            return $input;
    }

// Example
$fruit = array(
        'orange' => 'orange',
        'lemon' => 'yellow',
        'lime' => 'green',
        'grape' => 'purple',
        'cherry' => 'red',
);

// Replace lemon and lime with apple
array_splice_assoc($fruit, 'lemon', 'grape', array('apple' => 'red'));

// Replace cherry with strawberry
array_splice_assoc($fruit, 'cherry', 1, array('strawberry' => 'red'));

#7


0  

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

阅读更多关于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

阅读更多关于array_splice的信息:http://php.net/manual/en/function.array-splice.php

#1


276  

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

#2


21  

Use unset():

使用设置():

unset($array['key1']);

#3


13  

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

#4


4  

Using unset:

使用设置:

unset($array['key1'])

#5


1  

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

根据您的数组,您可能需要两个或多个循环:

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

foreach ($arr as $key1 => $values) {
  foreach ($key1 as $key2 => $value) {
  unset($arr[$key1][$key2]);
  }
}

#6


0  

Below is a method that removes items from an associative with offset, length and replacement - using array_splice

下面是一个方法,使用array_splice从关联式中删除带有偏移量、长度和替换的项

    function array_splice_assoc(&$input, $offset, $length = 1, $replacement = []) {
        $replacement = (array) $replacement;
        $key_indices = array_flip(array_keys($input));
        if (isset($input[$offset]) && is_string($offset)) {
           $offset = $key_indices[$offset];
        }
        if (isset($input[$length]) && is_string($length)) {
            $length = $key_indices[$length] - $offset;
        }

        $input = array_slice($input, 0, $offset, TRUE) + $replacement + array_slice($input, $offset + $length, NULL, TRUE);
            return $input;
    }

// Example
$fruit = array(
        'orange' => 'orange',
        'lemon' => 'yellow',
        'lime' => 'green',
        'grape' => 'purple',
        'cherry' => 'red',
);

// Replace lemon and lime with apple
array_splice_assoc($fruit, 'lemon', 'grape', array('apple' => 'red'));

// Replace cherry with strawberry
array_splice_assoc($fruit, 'cherry', 1, array('strawberry' => 'red'));

#7


0  

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

阅读更多关于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

阅读更多关于array_splice的信息:http://php.net/manual/en/function.array-splice.php