How to remove key value in 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

225k40 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
[
]

Pang

9,212146 gold badges85 silver badges118 bronze badges

answered Jun 16, 2010 at 13:15

SarfrazSarfraz

371k74 gold badges529 silver badges574 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

BafiBafi

5164 silver badges7 bronze badges

3

Use unset[]:

unset[$array['key1']];

Pang

9,212146 gold badges85 silver badges118 bronze badges

answered Jun 16, 2010 at 13:15

cletuscletus

604k163 gold badges903 silver badges940 bronze badges

0

Using unset:

unset[$array['key1']]

user229044

225k40 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: //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: //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]];
  }
}

answered Mar 6, 2015 at 5:28

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

OMROMR

10.9k5 gold badges13 silver badges30 bronze badges

How do you remove one key from an 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 a key from an associative array?

Given an associative array containing array elements and the task is to remove a key and its value from the associative array. Method 1: Using unset[] function: The unset[] function is used to unset a key and its value in an associative array. print_r[ $arr ];

How do you delete associative array in PHP?

Answer: Use the PHP unset[] Function If you want to delete an element from an array you can simply use the unset[] function. The following example shows how to delete an element from an associative array and numeric array.

What does Array_splice [] function do give an example?

The array_splice[] function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Tip: If the function does not remove any elements [length=0], the replaced array will be inserted from the position of the start parameter [See Example 2].

Chủ Đề