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
129
votes
5 answers

Are there dictionaries in php?

For example: $names = {[bob:27, billy:43, sam:76]}; and then be able to reference it like this: $names[bob]
bzupnick
  • 2,646
  • 4
  • 25
  • 34
123
votes
11 answers

Using an integer as a key in an associative array in JavaScript

When I create a new JavaScript array, and use an integer as a key, each element of that array up to the integer is created as undefined. For example: var test = new Array(); test[2300] = 'Some string'; console.log(test); will output 2298…
user243540
115
votes
3 answers

Convert an associative array to a simple array of its values in php

I would like to convert the array: Array ( [category] => category [post_tag] => post_tag [nav_menu] => nav_menu [link_category] => link_category [post_format] => post_format ) to array(category, post_tag, nav_menu, link_category,…
ItsGeorge
  • 2,060
  • 3
  • 17
  • 33
113
votes
5 answers

Adding an item to an associative array

//go through each question foreach($file_data as $value) { //separate the string by pipes and place in variables list($category, $question) = explode('|', $value); //place in assoc array $data = array($category => $question); …
Phil
  • 10,948
  • 17
  • 69
  • 101
112
votes
10 answers

Associative arrays: error "declare: -A: invalid option"

I've written a script that uses associative arrays in bash (v 4). It works fine on my local machine which is using 4.1.5(1)-release. On the production machine, using 4.1.0(1)-release the following line, which declares the assoc array, fails: declare…
Joel
  • 29,538
  • 35
  • 110
  • 138
112
votes
6 answers

Is there a way to create key-value pairs in Bash script?

I am trying to create a dictionary of key value pair using Bash script. I am trying using this logic: declare -d dictionary defaults write "$dictionary" key -string "$value" ...where $dictionary is a variable, but this is not working. Is there a…
RKS
  • 1,333
  • 2
  • 12
  • 12
107
votes
11 answers

Merge two numerically-keyed associative arrays and preserve the original keys

I have two arrays like this: array( '11' => '11', '22' => '22', '33' => '33', '44' => '44' ); array( '44' => '44', '55' => '55', '66' => '66', '77' => '77' ); I want to combine these two array such that it does not contains duplicate and as well…
Awan
  • 18,096
  • 36
  • 89
  • 131
100
votes
4 answers

Hash Table/Associative Array in VBA

I can't seem to find the documentation explaining how to create a hash table or associative array in VBA. Is it even possible? Can you link to an article or better yet post the code?
Tyler
  • 4,679
  • 12
  • 41
  • 60
97
votes
3 answers

How to make a list of associative array in yaml

I'm trying to store some configuration variables in yaml represented as an associative array aka dictionary. Here is how I did: content_prices: …
Antzi
  • 12,831
  • 7
  • 48
  • 74
97
votes
8 answers

PHP combine two associative arrays into one array

$array1 = array("$name1" => "$id1"); $array2 = array("$name2" => "$id2", "$name3" => "$id3"); I need a new array combining all together, i.e. it would be $array3 = array("$name1" => "$id1", "$name2" => "$id2", "$name3" => "$id3"); What is the…
jsteinmann
  • 4,502
  • 3
  • 17
  • 21
92
votes
10 answers

Multi-dimensional associative arrays in JavaScript

There is the following query results: (key1 and key2 could be any text) id key1 key2 value 1 fred apple 2 2 mary orange 10 3 fred banana 7 4 fred orange 4 5 sarah melon 5 ... and I wish to…
Omiod
  • 11,285
  • 11
  • 53
  • 59
90
votes
5 answers

Hash tables VS associative arrays

Recently I have read about hash-tables in a very famous book "Introduction to Algorithms". I haven't used them in any real applications yet, but want to. But I don't know how to start. Can anyone give me some samples of using it, for example, how to…
Bakhtiyor
  • 7,198
  • 15
  • 55
  • 77
87
votes
7 answers

How to create an associative array in JavaScript literal notation

I understand that there are no associative arrays in JavaScript, only objects. However I can create an array with string keys using bracket notation like this: var myArray = []; myArray['a'] = 200; myArray['b'] = 300; console.log(myArray); // Prints…
Wild Widow
  • 2,359
  • 3
  • 22
  • 34
81
votes
5 answers

JavaScript associative array to JSON

How can I convert a JavaScript associative array into JSON? I have tried the following: var AssocArray = new Array(); AssocArray["a"] = "The letter A" console.log("a = " + AssocArray["a"]); // result: "a = The letter…
ActionOwl
  • 1,473
  • 2
  • 15
  • 20
81
votes
11 answers

php: how to get associative array key from numeric index?

If I have: $array = array( 'one' =>'value', 'two' => 'value2' ); how do I get the string one back from $array[1] ?
Mazatec
  • 11,481
  • 23
  • 72
  • 108