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
21
votes
1 answer

Javascript: Best way to convert associative array to string and back the other way later?

I have an associative array as follows: var AssocArray = { id:0, folder:'Next', text:'Apple' }; Now I need to store this in a database, so I figure I would just convert this into a string, store it in the database and then pull it out of the…
Mark
  • 3,653
  • 10
  • 30
  • 62
20
votes
2 answers

Using an associative array as data for D3

I have a very simple D3 example that first reads data into an associative array, then displays it in a bar graph. I can't seem to get anything to display using this method though. Instead, I have to insert a task in between: Read the data into an…
Malcolm Crum
  • 4,345
  • 4
  • 30
  • 49
20
votes
4 answers

How to sort a date array in PHP

I have an array in this format: Array ( [0] => Array ( [28th February, 2009] => 'bla' ) [1] => Array ( [19th March, 2009] => 'bla' ) [2] => Array ( [5th April,…
Ali
  • 261,656
  • 265
  • 575
  • 769
19
votes
5 answers

Validate dicts in Python

i looking for tool, or examples to/how to validate dictionaries in python. For example, i have dict: test = {'foo' : 'bar', 'nested' : {'foo1' : 'bar1', 'foo2' : 'bar2'} } And now i must validate it. Lets say, value for key foo must be boolean…
Galmi
  • 743
  • 1
  • 11
  • 18
19
votes
6 answers

Read associative array from json in $_POST

I am using jQuery to post a json object to my php application. jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); }); The json string as pulled from firebug looks like this { "data" : [ { "contents" : "This is some…
Daniel
  • 407
  • 2
  • 5
  • 10
19
votes
2 answers

Efficient syntax for populating a javascript associative array

I have an autocomplete text box that users can type an item code into and need to find out what the id number of that item code is in javascript. An associative array is the way I would imagine it should be done, but the following seems a little…
Jimbo
  • 22,379
  • 42
  • 117
  • 159
19
votes
10 answers

Change key in associative array in PHP

Say I have an array like this: array(2) { [0]=> array(2) { ["n"]=> string(4) "john" ["l"]=> string(3) "red" } [1]=> array(2) { ["n"]=> string(5) "nicel" ["l"]=> string(4) "blue" } } How would I change the keys of the inside…
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
19
votes
4 answers

Remove all elements of an array with non-numeric keys

I have an array that looks something like this: Array ( [0] => apple ["b"] => banana [3] => cow ["wrench"] => duck ) I want to take that array and use array_filter or something similar to remove elements with non-numeric keys and…
diracdeltafunk
  • 1,178
  • 2
  • 10
  • 24
18
votes
3 answers

Bash associative array with list as value

I have to work with an output of a Java tool, which returns a map data structure that looks like HashMap. I have to work with BASH and i tried to declare it as an associative array, what is very similar to a map. The…
Oni1
  • 1,445
  • 2
  • 19
  • 39
18
votes
4 answers

PHP: Using spaces in associative array indices

Is this bad practice/can cause problems? $_SESSION['stuff to keep'] As opposed to calling str_replace() on the indices.
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
17
votes
6 answers

JavaScript: Adding to an associative array

I have a function called insert which takes two parameters (name and telnumber). When I call this function I want to add to an associative array. So for example, when I do the following: insert("John", "999"); insert("Adam", "5433"); I want to it…
ritch
  • 1,760
  • 14
  • 37
  • 65
17
votes
2 answers

Extracting a subset of values from an associative array (php)

I want to do something seemingly very simple, but I can't find anything about it: simply extract a subset of an array similar to array_splice, but using keys to retrieve the values : $data = array('personName' => 'John', 'personAge' => 99,…
user58777
17
votes
2 answers

Why can't I store string keys in an Associative Array?

I'm new to D programming language, just started reading The D Programming Language book. I run into error when trying one associative array example code #!/usr/bin/rdmd import std.stdio, std.string; void main() { uint[string] dict; foreach…
Visus Zhao
  • 1,144
  • 2
  • 12
  • 25
16
votes
5 answers

PHP Count Number of True Values in a Boolean Array

I have an associative array in which I need to count the number of boolean true values within. The end result is to create an if statement in which would return true when only one true value exists within the array. It would need to return false if…
ThatTechGuy
  • 879
  • 1
  • 10
  • 29
16
votes
3 answers

How to join an associative array into a string

I am trying to do this now and I wonder if there is a "the most used" method to join an associative array (it's values) into a string, delimited by a character. For example, I have var AssocArray = { id:0, status:false, text:'apple' }; The string…
ali
  • 10,927
  • 20
  • 89
  • 138