Questions tagged [associative-array]

An associative array is an abstract data type composed of a collection of unique keys mapped to a collection of values.

An associative array (also associative container, map, mapping, dictionary, finite map, and in query-processing an index or index file) is an abstract data type composed of a collection of unique keys and a collection of values, where each key is associated with one value (or set of values). The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by an associative array. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key "bob" is 7, we say that our array maps "bob" to 7. Associative arrays are very closely related to the mathematical concept of a function with a finite domain. As a consequence, a common and important use of associative arrays is in memoization.

See also

3174 questions
814
votes
15 answers

How to define hash tables in Bash?

What is the equivalent of Python dictionaries but in Bash (should work across OS X and Linux).
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
750
votes
8 answers

How to check if a specific key is present in a hash or not?

I want to check whether the "user" key is present or not in the session hash. How can I do this? Note that I don't want to check whether the key's value is nil or not. I just want to check whether the "user" key is present.
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
715
votes
18 answers

How do I remove objects from a JavaScript associative array?

Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"] = "Smith"; myArray["age"] = 25; Now if I wanted to remove "lastname"?....is there some equivalent of myArray["lastname"].remove()? (I need the…
Andrew
454
votes
14 answers

Extract a subset of key-value pairs from dictionary?

I have a big dictionary object that has several key value pairs (about 16), but I am only interested in 3 of them. What is the best way (shortest/efficient/most elegant) to subset such dictionary? The best I know is: bigdict =…
Jayesh
  • 51,787
  • 22
  • 76
  • 99
438
votes
5 answers

How to iterate over associative arrays in Bash

Based on an associative array in a Bash script, I need to iterate over it to get the key and value. #!/bin/bash declare -A array array[foo]=bar array[bar]=foo I actually don't understand how to get the key while using a for-in loop.
pex
  • 7,351
  • 4
  • 32
  • 41
419
votes
25 answers

Replace keys in an array based on another lookup/mapping array

I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an ID number and the value is a count. This is fine for most instances, however I want a…
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
232
votes
15 answers

Java associative-array

How can I create and fetch associative arrays in Java like I can in PHP? For example: $arr[0]['name'] = 'demo'; $arr[0]['fname'] = 'fdemo'; $arr[1]['name'] = 'test'; $arr[1]['fname'] = 'fname';
dobs
  • 2,742
  • 2
  • 21
  • 19
221
votes
4 answers

Union of dict objects in Python

How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless there are duplicates)? For example, the union of {'a' : 0, 'b' : 1} and {'c' : 2} is {'a' : 0, 'b' :…
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
208
votes
11 answers

How to loop through an associative array and get the key?

My associative array: $arr = array( 1 => "Value1", 2 => "Value2", 10 => "Value10" ); Using the following code, $v is filled with $arr's values foreach ($arr as $v){ echo $v; // Value1, Value2, Value10 } How do I get $arr's keys…
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
207
votes
10 answers

Dynamically creating keys in a JavaScript associative array

All the documentation I've found so far is to update keys that are already created: arr['key'] = val; I have a string like this: " name = oscar " And I want to end up with something like this: { name: 'whatever' } That is, split the string and…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
184
votes
16 answers

Custom key-sort a flat associative based on another array

Is it possible in PHP to do something like this? How would you go about writing a function? Here is an example. The order is the most important thing. $customer['address'] = '123 fake st'; $customer['name'] = 'Tim'; $customer['dob'] =…
alex
  • 479,566
  • 201
  • 878
  • 984
153
votes
14 answers

How to concatenate properties from multiple JavaScript objects

I am looking for the best way to "add" multiple JavaScript objects (associative arrays). For example, given: a = { "one" : 1, "two" : 2 }; b = { "three" : 3 }; c = { "four" : 4, "five" : 5 }; what is the best way to compute: { "one" : 1, "two" : 2,…
Vlad
  • 9,180
  • 5
  • 48
  • 67
152
votes
17 answers

Associative arrays in shell scripts

We require a script that simulates associative arrays or map-like data structure for shell scripting. Can anyone let's know how it is done?
Irfan Zulfiqar
  • 2,029
  • 5
  • 16
  • 21
135
votes
11 answers

Fastest way to implode an associative array with keys

I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use '&' for xhtml links or '&' otherwise. My first inclination is to use foreach…
matpie
  • 17,033
  • 9
  • 61
  • 82
131
votes
6 answers

Prepend associative array elements to an associative array

Is it possible to prepend an associative array with literal key=>value pairs? I know that array_unshift() works with numerical keys, but I'm hoping for something that will work with literal keys. As an example I'd like to do the following: $array1 =…
Colin Brock
  • 21,267
  • 9
  • 46
  • 61
1
2 3
99 100