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
15
votes
4 answers

Using variable as a key in an bash associative array

I'm trying to read the English dictionary in Linux into an associative array, using the words as keys and and predefined string as a value. This way I can look up words by key to see if they exist. Also I need all to words to be lowercase. It's…
Asgeir
  • 727
  • 3
  • 9
  • 20
15
votes
3 answers

PHP Count function with Associative Array

Could someone please explain to me how the count function works with arrays like the one below? My thought would be the following code to output 4, cause there are 4 elements there: $a = array ( "1" => "A", 1=> "B", "C", 2…
Parampal Pooni
  • 2,958
  • 8
  • 34
  • 40
15
votes
4 answers

How does jQuery.each() work with associative arrays (objects)?

I have an associative array with two object inside. Running this through $(myassoc).each(), the callback runs only once. Also the callback parameters (index and object) returns 0 and the entire associative array, respectively. One would expect…
Hubro
  • 56,214
  • 69
  • 228
  • 381
15
votes
1 answer

Creating an associative array in Rust

I was looking for the way to create an associative array in the documentation but didn't find anything. So how do I create an associative array in Rust?
Incerteza
  • 32,326
  • 47
  • 154
  • 261
15
votes
6 answers

How can I use array_map with keys and values, but return an array with the same indexes (not int)?

I have an array such as ['id' => 1, 'name' => 'Fred']. I want to call array_map on this array and also use the key inside the function. However, when I make a return, my keys will become int. Simple example : $arr = array('id' => 1, 'name' =>…
user2816456
  • 569
  • 2
  • 5
  • 15
15
votes
6 answers

Change $key of associative array in a foreach loop in php

I have an array like this: array( 'firstName' => 'Joe', 'lastName' => 'Smith' ) I need to loop over every element in my array and in the end, the array should look like this: array( 'FirstName' => 'Joe', 'LastName' => 'Smith' …
brianwalleshauser
  • 423
  • 2
  • 5
  • 12
15
votes
5 answers

create an associative array in jquery

This is what I have so far and the shoe types are boots, wellingtons, leather, trainers (in that order) I want to iterate through and assign the value so I haves something like var shoeArray = { boots : '3', wellingtons: '0', leather : '1',…
LeBlaireau
  • 17,133
  • 33
  • 112
  • 192
15
votes
3 answers

php insert value into array of arrays using foreach

I have a pretty basic question but I am stuck. I am pretty new to php and I have an array like this: $array = array( 'one' => 1, 'two' => array('key1' => 'val1','key2' => 'val2'), 'three' => array('key1' => 'val1','key2' => 'val2'), …
Benjamin Thvedt
  • 351
  • 1
  • 3
  • 17
15
votes
2 answers

Why is this check for null associative array in PL/SQL failing?

I have an associative array created by a type of rowtype of a table column. To give an example, this is how it is(the table names are different, but the structure is the same): This is the DDL of the table CREATE TABLE employees ( id …
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
15
votes
8 answers

java and python equivalent of php's foreach($array as $key => $value)

In php, one can handle a list of state names and their abbreviations with an associative array like this: "AL", "ALASKA"=>"AK", // etc... "WYOMING"=>"WY" ); foreach…
Lasoldo Solsifa
  • 375
  • 3
  • 5
  • 14
14
votes
3 answers

JS associative arrays: add new pair

I have an associative array in JS. var array = { 'one' : 'first', 'two' : 'second', 'three' : 'third' }; How can I add new pair in it
Artur Keyan
  • 7,643
  • 12
  • 52
  • 65
14
votes
1 answer

Convert associative array to numeric array

I have the following associative array structure in JavaScript Array ( [-1] => Array ( catId : -1 [subCatId] => Array ( subCatId : -3 [0] => Array ( property : value ) …
Kalpesh Jain
  • 409
  • 3
  • 10
  • 19
14
votes
3 answers

PL/SQL: Selecting from a table into an assoc array

I am trying to select data into a pl/sql associative array in one query. I know I can do this with a hardcoded key, but I wanted to see if there was some way I could reference another column (the key column) instead. DECLARE TYPE VarAssoc IS TABLE…
Seaux
  • 3,459
  • 2
  • 28
  • 27
14
votes
6 answers

Bash array assignment fails if you declare the array in advance

This works: $ BAR=(a b c) $ echo "${BAR[1]}" b This, however, doesn't: $ declare -A FOO $ FOO=(a b c) bash: FOO: a: must use subscript when assigning associative array bash: FOO: b: must use subscript when assigning associative array bash: FOO: c:…
Thanatos
  • 42,585
  • 14
  • 91
  • 146
14
votes
2 answers

How to insert a new key and value in multidimensional array?

Following is the output of my multidimensional array $csmap_data Array ( [0] => Array ( [cs_map_id] => 84 [cs_subject_id] => 1 ) [1] => Array ( [cs_map_id] => 85 …
PHPLover
  • 1
  • 51
  • 158
  • 311