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
46
votes
9 answers

Compare 2-dimensional data sets based on a specified second level value

I have an array containing rows of associative data. $array1 = array( array('ITEM' => 1), array('ITEM' => 2), array('ITEM' => 3), ); I have a second array, also containing rows of associative data, that I would like to filter using the…
45
votes
11 answers

Value objects vs associative arrays in PHP

(This question uses PHP as context but isn't restricted to PHP only. e.g. Any language with built in hash is also relevant) Let's look at this example (PHP): function makeAFredUsingAssoc() { return array( 'id'=>1337, …
kizzx2
  • 18,775
  • 14
  • 76
  • 83
43
votes
4 answers

How to insert a new key value pair in array in php?

I've an array as follows named $test_package_data. For the reference I'm printing first two elements of it: Array ( [0] => Array ( [test_pack_id] => 9f27643023a83addd5eed41c4aade840 [test_pack_name] => Exams…
PHPLover
  • 1
  • 51
  • 158
  • 311
42
votes
8 answers

How are JavaScript arrays implemented?

Namely, how does the following code: var sup = new Array(5); sup[0] = 'z3ero'; sup[1] = 'o3ne'; sup[4] = 'f3our'; document.write(sup.length + "
"); output '5' for the length, when all you've done is set various elements? My 'problem' with this…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
41
votes
9 answers

Return all array elements except for a given key

Simple one, I was just wondering if there is a clean and eloquent way of returning all values from an associative array that do not match a given key(s)? $array = array('alpha' => 'apple', 'beta' => 'banana', 'gamma' => 'guava'); $alphaAndGamma =…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
41
votes
1 answer

Javascript: Checking if an object has no properties or if a map/associative-array is empty

Possible Duplicate: How do I test for an empty Javascript object from JSON? Is there an easy way to check if an object has no properties, in Javascript? Or in other words, an easy way to check if a map/associative array is empty? For example,…
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
38
votes
3 answers

Using constants as indices for JavaScript associative arrays

I'm looking to create an associative array in JavaScript, but use constants defined as part of the class as indices. The reason I want this is so that users of the class can use the constants (which define events) to trigger actions. Some code to…
Edan Maor
  • 9,772
  • 17
  • 62
  • 92
37
votes
5 answers

Return first key of associative array in PHP

I'm trying to obtain the first key of an associative array, without creating a temporary variable via array_keys() or the like, to pass by reference. Unfortunately both reset() and array_shift() take the array argument by reference, so neither seem…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
37
votes
8 answers

Does powershell have associative arrays?

I am writing a function that returns an id, name pair. I would like to do something like $a = get-name-id-pair() $a.Id $a.Name like is possible in javascript. Or at least $a = get-name-id-pair() $a["id"] $a["name"] like is possible in php. Can I…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
36
votes
4 answers

Comment associative array in PHP Documentor

I use several associative arrays in my PHP application and I'm using PHP documentor to comment my sources. I never really did specify comments for the arrays in an array, but now I need to do that and don't know how. $array = array('id' => 'test',…
Abenil
  • 1,048
  • 3
  • 12
  • 26
35
votes
2 answers

Multidimensional associative arrays in Bash

I'm trying to create a multidimensional associative array but need some help. I have reviewed the page suggested in this SO answer but it confused me even more. So far here is what I have: The script: #!/bin/bash declare -A PERSONS declare -A…
Max
  • 12,794
  • 30
  • 90
  • 142
34
votes
3 answers

Associative Array versus SplObjectStorage

I'm working on code to manage a collection of unique objects. The first prototype of this code utilises an associative array, basically as that's the way I've always done it. However, I'm also keen on taking advantage of functionality that's been…
GordonM
  • 31,179
  • 15
  • 87
  • 129
34
votes
3 answers

What's the difference between pls_integer and binary_integer?

I've inherited some code which is going to be the base for some additional work. Looking at the stored procs, I see quite a lot of associative-arrays. Some of these are indexed by binary_integers, some by pls_integers. Are there any differences…
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
33
votes
3 answers

Redis how to store associative array? Set or Hash or List?

I'm a bit confused with all the available storing options of Redis. I want to do something simple and I don't want to over engineer it. I'm working with phpredis and Redis v2.8.6. I have this simple associative array that I need to store. I also…
maxwell2022
  • 2,818
  • 5
  • 41
  • 60
32
votes
5 answers

JavaScript associate array

In Python I could do something like myMap = {key: [value1, value2]} and then access the value2 using myMap[key][1] Can I do something like this in JavaScript?
MxLDevs
  • 19,048
  • 36
  • 123
  • 194