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

Popping the key & value from an associative array in PHP

Let S be an associative array in PHP, I need to retrieve and extract from it the first element, both the value and the key. I would use value1=array_pop(S); but it only gives me the value. I can use…
Pietro Speroni
  • 3,131
  • 11
  • 44
  • 55
25
votes
2 answers

Bash associative array size

Is there a way to get size of associative array in bash: declare -A array ...without iterating through the elements? The size of interest is both: just number of elements, and memory amount it consumes?
wick
  • 1,995
  • 2
  • 20
  • 31
24
votes
4 answers

Selecting a random element from a PHP associative array

I've got an associative array in PHP and want to select a random key/value pair out of it. Here's what I have so far: Initialize. $locations = array(); Loops through a SQL query and adds key/val pairs: array_push($locations, "'$location_id' =>…
Jeff
  • 717
  • 2
  • 8
  • 19
24
votes
4 answers

Get value without knowing key in one-pair-associative-array

There is an associative array with only one pair key=>value. I don't know it's key, but I need to get it's value: $array = array('???' => 'value'); $value = // ?? $array[0] doesn't work. How can I get it's value?
Qiao
  • 16,565
  • 29
  • 90
  • 117
22
votes
8 answers

How to rename an associative array in Bash?

I need to loop over an associative array and drain the contents of it to a temp array (and perform some update to the value). The leftover contents of the first array should then be discarded and i want to assign the temp array to the original array…
David Parks
  • 30,789
  • 47
  • 185
  • 328
22
votes
3 answers

array_unique for arrays inside array

I need a function like array_unique for arrays inside array. The Case - should be equal, but output "not equal":
Ben
  • 25,389
  • 34
  • 109
  • 165
22
votes
5 answers

Awk array iteration for multi-dimensional arrays

Awk offers associative indexing for array processing. Elements of 1 dimensional array can be iterated: e.g. for(index in arr1) print "arr1[" index "]=" arr1[index] But how this kind done for a two dimensional array? Does kind of syntax,given…
cobp
  • 772
  • 1
  • 5
  • 19
22
votes
4 answers

In Bash test if associative array is declared

How can I test if an associative array is declared in Bash? I can test for a variable like: [ -z $FOO ] && echo nope but I doesn't seem to work for associative arrays: $ unset FOO $ declare -A FOO $ [ -z $FOO ] && echo nope nope $ FOO=([1]=foo) $ […
James Brown
  • 36,089
  • 7
  • 43
  • 59
22
votes
7 answers

How to use PHP in_array with associative array?

Is there any php function such as in_array for associative arrays you get by the mysql function "mysql_fetch assoc" ? For example, if I have an $array that looks like this: array(0=>(array(ID=>1, name=>"Smith"), 1=>(array(ID=>2, name=>"John")) Can…
Jacob Cohen
  • 1,232
  • 3
  • 13
  • 33
22
votes
5 answers

Difference between array_filter() and array_map()?

I looked into the similar topics in web as well stack overflow, but could get this one into my head clearly. Difference between array_map, array_walk and array_filter
John Cooper
  • 7,343
  • 31
  • 80
  • 100
22
votes
4 answers

Function to sort an array and return the sorted array

PHP's native sorting functions modify by reference and do not return the sorted array. I am looking for a reliable standard method to sort an array, returning the sorted array as the return value. All of the PHP.net functions I have read about…
István Pálinkás
  • 2,217
  • 7
  • 25
  • 50
22
votes
2 answers

PHP - associative array as an object

Possible Duplicate: Convert Array to Object PHP I'm creating a simple PHP application and I would like to use YAML files as a data storage. I will get the data as an associative array, with this structure for example: $user = array('username' =>…
Martin Majer
  • 3,274
  • 4
  • 23
  • 36
21
votes
5 answers

How to determine if an associative array has a key?

In ActionScript 3, is there any convenient way of determining if an associative array (dictionary) has a particular key? I need to perform additional logic if the key is missing. I could catch the undefined property exception, but I'm hoping that…
Soviut
  • 88,194
  • 49
  • 192
  • 260
21
votes
7 answers

Iterating over a complex Associative Array in PHP

Is there an easy way to iterate over an associative array of this structure in PHP: The array $searches has a numbered index, with between 4 and 5 associative parts. So I not only need to iterate over $searches[0] through $searches[n], but also…
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
21
votes
11 answers

Isolate a single column in a multi-dimensional array

Say for example you just queried a database and you recieved this 2D array. $results = array( array('id' => 1, 'name' => 'red' , 'spin' => 1), array('id' => 2, 'name' => 'green', 'spin' => -1), array('id' => 3, 'name' => 'blue' ,…
gradbot
  • 13,732
  • 5
  • 36
  • 69