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
32
votes
13 answers

How to add an array value to the middle of an associative array?

Lets say I have this array: $array = array('a'=>1,'z'=>2,'d'=>4); Later in the script, I want to add the value 'c'=>3 before 'z'. How can I do this? Yes, the order is important. When I run a foreach() through the array, I do NOT want this newly…
Citizen
  • 12,430
  • 26
  • 76
  • 117
32
votes
6 answers

Replace item in association list in elisp

I have an alist in emacs lisp like: (setq a1 '((:k1 . 1) (:k2 . 2) (:k3 . 3))) and i want to change value of :k1 to 10, like (:k1 . 10). How do i do that? I tried (setf (assoc :k1 a1) '(:k1 . 10)) - it didn't work.
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
31
votes
6 answers

Bash associative array sorting by value

I get the following output: Pushkin - 100500 Gogol - 23 Dostoyevsky - 9999 Which is the result of the following script: for k in "${!authors[@]}" do echo $k ' - ' ${authors["$k"]} done All I want is to get the output like this: Pushkin -…
Graf
  • 1,437
  • 3
  • 17
  • 27
30
votes
7 answers

variable as index in an associative array - Javascript

I'm trying to create an associative array, create an empty array, and then add a (indexName -> value) pair: var arrayName = new Array; arrayName["indexName"] = value; // i know i can also do the last line like this: arrayName.indexName =…
bogdan
  • 1,269
  • 3
  • 12
  • 18
30
votes
4 answers

PHP - Merge two arrays (same-length) into one associative?

pretty straightforward question actually.. is it possible in PHP to combine two separate arrays of the same length to one associative array where the values of the first array are used as keys in the associative array? I could ofcourse do this,…
Ropstah
  • 17,538
  • 24
  • 120
  • 194
29
votes
1 answer

Get nth key of associative php array

I want to get the value of the KEY of an associative PHP array at a specific entry. Specifically, I know the KEY I need is the key to the second entry in the array. Example: $array = array('customer' => 'Joe', 'phone' => '555-555-5555'); What…
jtubre
  • 669
  • 1
  • 8
  • 13
29
votes
3 answers

Foreach loop in jade (node.js template engine)

Ok, I am getting an associative array from node server and trying to render it in Jade. I obviously need a foreach loop, but nothing seems to work! I tried these both codes: - foreach row in rows { li= row - } and - rows.forEach(function(item))…
user1130217
28
votes
5 answers

How do you initialize a map which takes a struct as value?

I am using a map as an associative array of IDs -> value, where the value is a struct defining the object: #include struct category { int id; std::string name; }; std::map categories; int main() { …
augustin
  • 14,373
  • 13
  • 66
  • 79
28
votes
6 answers

array_reduce() can't work as associative-array "reducer" for PHP?

I have an associative array $assoc, and need to reduce to it to a string, in this context $OUT = "$v) $OUT.= " $k=\"$v\""; $OUT.= '/>'; How to do in an elegant way the same thing, but using array_reduce() Near the…
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
28
votes
0 answers

Can anyone recommend a good Hashtable implementation in Javascript?

I've found jCache and some other home-grown methods using associative arrays. If you've had experience with jCache, were there any limitations?
David Robbins
  • 9,996
  • 7
  • 51
  • 82
27
votes
2 answers

array_push for associative arrays

I'm trying to extend an assoc array like this, but PHP doesn't like it. I receive this message: Warning: array_push() expects parameter 1 to be array, null given Here's my code: $newArray = array(); foreach ( $array as $key => $value ) { …
EnglishAdam
  • 1,380
  • 1
  • 19
  • 42
27
votes
8 answers

Automatically sorted by values map in Java

I need to have an automatically sorted-by-values map in Java - so that It keeps being sorted at any time while I'm adding new key-value pairs or update the value of an existing key-value pair, or even delete some entry. Please also have in mind…
Alexandros
  • 4,425
  • 4
  • 23
  • 21
27
votes
2 answers

PHP: Printing Associative Array

In PHP, I have an associative array like this $a = array('who' => 'one', 'are' => 'two', 'you' => 'three'); How to write a foreach loop that goes through the array and access the array key and value so that I can manipulate them (in other words, I…
Tu Hoang
  • 4,622
  • 13
  • 35
  • 48
27
votes
7 answers

php array_merge associative arrays

I'm trying to prepend an item to the beginning of an associative array. I figured the best way to do this is to use array_merge, but I'm having some odd consequences. I get the id and Name of products from a mysql database, and it gets returned as…
stephenbayer
  • 12,373
  • 15
  • 63
  • 98
27
votes
8 answers

In PHP, is there a function that returns an array made up of the value of a key from an array of associative arrays?

I'm sure this question has been asked before, my apologies for not finding it first. The original array: [0] => Array ( [categoryId] => 1 [eventId] => 2 [eventName] => 3 [vendorName] => 4 ) [1] => Array …
Caleb Gray
  • 3,040
  • 2
  • 21
  • 32