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
69
votes
7 answers

Group rows in an associative array of associative arrays by column value and preserve the original first level keys

I have an array of subarrays in the following format: [ 'a' => ['id' => 20, 'name' => 'chimpanzee'], 'b' => ['id' => 40, 'name' => 'meeting'], 'c' => ['id' => 20, 'name' => 'dynasty'], 'd' => ['id' => 50, 'name' => 'chocolate'], …
Anson Kao
  • 5,256
  • 4
  • 28
  • 37
66
votes
2 answers

What is the easiest way to handle associative array in c#?

I do not have a lot of experience with C#, yet I am used of working with associative arrays in PHP. I see that in C# the List class and the Array are available, but I would like to associate some string keys. What is the easiest way to handle…
Michael
  • 4,786
  • 11
  • 45
  • 68
63
votes
10 answers

How to pass an associative array as argument to a function in Bash?

How do you pass an associative array as an argument to a function? Is this possible in Bash? The code below is not working as expected: function iterateArray { local ADATA="${@}" # associative array for key in "${!ADATA[@]}" do …
niksfirefly
  • 749
  • 1
  • 5
  • 4
62
votes
10 answers

Finding cartesian product with PHP associative arrays

Say that I have an array like the following: Array ( [arm] => Array ( [0] => A [1] => B [2] => C ) [gender] => Array ( [0] => Female [1] => Male ) …
Lotus Notes
  • 6,302
  • 7
  • 32
  • 47
62
votes
20 answers

Is there a way to find out how "deep" a PHP array is?

A PHP array can have arrays for its elements. And those arrays can have arrays and so on and so forth. Is there a way to find out the maximum nesting that exists in a PHP array? An example would be a function that returns 1 if the initial array does…
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
62
votes
4 answers

Change an associative array into an indexed array / get an Zend_Table_Row_Abstract as non-associative

Hi out there in Stackland. I was wondering if there was either a function or an easy way to change an associative array into an indexed array. To elaborate, I'm using the Zend framework, and I've got a point in my site where I take out a row of an…
Ethan
  • 5,660
  • 9
  • 44
  • 51
61
votes
4 answers

Interpolation (double quoted string) of Associative Arrays in PHP

When interpolating PHP's string-indexed array elements (5.3.3, Win32) the following behavior may be expected or not: $ha = array('key1' => 'Hello to me'); print $ha['key1']; # correct (usual way) print $ha[key1]; # Warning, works (use of…
rubber boots
  • 14,924
  • 5
  • 33
  • 44
57
votes
7 answers

Can PHP's list() work with associative arrays?

Example: list($fruit1, $fruit2) = array('apples', 'oranges'); code above of course works ok, but code below: list($fruit1, $fruit2) = array('fruit1' => 'apples', 'fruit2' => 'oranges'); gives: Notice: Undefined offset: 1 in.... Is there any way to…
rsk82
  • 28,217
  • 50
  • 150
  • 240
56
votes
7 answers

PHP: Get n-th item of an associative array

If you have an associative array: Array ( [uid] => Marvelous [status] => 1 [set_later] => Array ( [0] => 1 [1] => 0 ) [op] => Submit [submit] => Submit ) And you want to access the 2nd…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
51
votes
7 answers

Associative arrays in C

I am implementing a way to transfer a set of data to a programmable dongle. The dongle is based on a smart card technology and can execute an arbitrary code inside. The input and output data is passed as a binary blocks that can be accessed via…
ezpresso
  • 7,896
  • 13
  • 62
  • 94
50
votes
6 answers

Finding the minimum value's key in an associative array

In PHP, say that you have an associative array like this: $pets = array( "cats" => 1, "dogs" => 2, "fish" => 3 ); How would I find the key with the lowest value? Here, I'd be looking for cats. Is there some built in PHP function that…
Philip Morton
  • 129,733
  • 38
  • 88
  • 97
48
votes
2 answers

Interface for associative object array in TypeScript

I have an object like so: var obj = { key1: "apple", key2: true, key3: 123, . . . key{n}: ... } So obj can contain any number of named keys, but the values must all be either string, bool, or number. How do I declare the…
prmph
  • 7,616
  • 11
  • 37
  • 46
47
votes
2 answers

Delete vs splice on associative array

If I have a JS associative array which is from what I gather is really an object, and I wish to remove an element, using delete myArr[someId] will set the element to undefined, whilst splice won't work at all... so what is the alternative for an…
sarsnake
  • 26,667
  • 58
  • 180
  • 286
46
votes
9 answers

Display array values in PHP

So, I'm working with PHP for the first time and I am trying to retrieve and display the values of an array. After a lot of googling, the only methods I can find for this are print_r, var_dump or var_export. However, all of these methods return…
Thomas
  • 5,030
  • 20
  • 67
  • 100
46
votes
3 answers

Associative Arrays in Ruby

Does Ruby have associative arrays? For eg: a = Array.new a["Peter"] = 32 a["Quagmire"] = 'asdas' What is the easiest method to create such an data structure in Ruby?
Sushanth CS
  • 2,412
  • 3
  • 23
  • 23