Remove certain keys and values from associative array

Asked
Active3 hr before
Viewed126 times

7 Answers

certainvaluesarrayremove
90%

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" } , 26 +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. – FreeAsInBeer Jul 30 '12 at 21:20 ,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" } , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

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
   ()
load more v
88%

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.,Method 2: Using array_diff_key() function: This function is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them.,How to create comma separated list from an array in PHP ?

Examples:

Input: array("name" => "Anand", "roll" => "1")
Output: Array(
   [roll] => 1
)

Input: array("1" => "Add", "2" => "Multiply", "3" => "Divide")
Output: Array(
   [2] => Multiply[3] => Divide
)

Syntax:

void unset($array_name['key_to_be_removed'])
Output:
Array
   (
      [2] => Multiply[3] => Divide
   )

Syntax:

array array_diff_key($array_name, array_flip((array)['keys_to_be_removed'])
Output:
Array
   (
      [2] => b[3] => c
   )
load more v
72%

Search Answer Titles

$colors = array("blue", "green", "red");

//delete element in array by value "green"
if (($key = array_search("green", $colors)) !== false) {
   unset($colors[$key]);
}
load more v
65%

array_values — Return all the values of an array, array_values() returns all the values from the array and indexes the array numerically. ,array_combine() - Creates an array by using one array for keys and another for its values, Returns an indexed array of values.

Array
   (
      [0] => XL[1] => gold
   )
75%

array_unset(): Removing element from any position of the Array,Deleting array elements by using unsert(),unset : Deleting elements of an array by using its key or value ,array_map : Using user defined and native function to each element of an array


unset($input[3]);
load more v
40%

Binding keys to a value:,Finding the value for a set of keys: This operation looks up values that are bound to a single key or multiple keys.,An associative array is a map or look-up table consisting of a collection of keys and their associated values. There is a one-to-one mapping between a set of keys and values. Associative arrays are supported by Perl, ksh93, and several other languages.,All of the keys in delete() function can be specified as ANY, in which case, all elements of the associative array will be deleted. This function returns 0 if matching elements are found and are deleted. Otherwise, the delete() function returns a value of 1.

/* single key dimension */
count["ksh"] = 1;
/* multiple key dimensions */
var [0][“a”][2.5] = 1;
var [1][“a”][3.5] = 2;
load more v
22%

A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key/value pairs. For example, a hash table might contain a series of IP addresses and computer names, where the IP addresses are the keys and the computer names are the values, or vice versa.,The items in a hash table are intrinsically unordered. The key/value pairs might appear in a different order each time that you display them.,Enter one or more key/value pairs for the content of the hash table.,You cannot use a subtraction operator to remove a key/value pair from a hash table, but you can use the Remove method of the Hashtable object. The Remove method takes the key as its value.

The syntax of a hash table is as follows:

@{ <name> = <value>; [<name> = <value> ] ...}
load more v

Other "certain-values" queries related to "Remove certain keys and values from associative array"