This tag is used mostly regarding associative arrays (but better use tag "associative-array" for that) and sometimes in the mathematic sense of tag "associativity", the associative property of binary operations.
Questions tagged [associative]
435 questions
715
votes
18 answers
How do I remove objects from a JavaScript associative array?
Suppose I have this code:
var myArray = new Object();
myArray["firstname"] = "Bob";
myArray["lastname"] = "Smith";
myArray["age"] = 25;
Now if I wanted to remove "lastname"?....is there some equivalent of
myArray["lastname"].remove()?
(I need the…
Andrew
88
votes
11 answers
How to sort an associative array by its values in Javascript?
I have the associative array:
array["sub2"] = 1;
array["sub0"] = -1;
array["sub1"] = 0;
array["sub3"] = 1;
array["sub4"] = 0;
What is the most elegant way to sort (descending) by its values where the result would be an array with the respective…

John Smith
- 8,567
- 13
- 51
- 74
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
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
7 answers
CSV to Associative Array
I've seen numerous examples on how to take a CSV file and then create an associative array with the headers as the keys.
For example:
Brand,Model,Part,Test
Honda,Civic,123,244
Honda,Civic,135,434
Toyota,Supra,511,664
Where it would create an Array…

ParoX
- 5,685
- 23
- 81
- 152
59
votes
2 answers
Initialize an Associative Array with Key Names but Empty Values
I cannot find any examples, in books or on the web, describing how one would properly initialize an associative array by name only (with empty values) - unless, of course, this IS the proper way(?)
It just feels as though there is another more…

NYCBilly
- 870
- 1
- 8
- 11
46
votes
3 answers
What is the most efficient way to get the first item from an associative array in JavaScript?
I need to get just the first item (actually, just the first key) off a rather large associative array in JavaScript. Here's how I'm doing it currently (using jQuery):
getKey = function (data) {
var firstKey;
$.each(data, function (key, val)…

Andrew Hedges
- 21,688
- 16
- 67
- 79
41
votes
11 answers
Is array both associative and indexed?
Can an array in JavaScript be associative AND indexed?
I'd like to be able to lookup an item in the array by its position or a key value.

puffpio
- 3,402
- 6
- 36
- 41
38
votes
1 answer
How to make a right-associative infix operator?
I have an associative operation >>. The problem is that its cost linearly depends on the size of its left operand. So an expression formed by a sequence of n applications of >> like
a >> a >> a >> a >> a >> ... >> a
it has quadratic cost in terms…

Petr
- 62,528
- 13
- 153
- 317
37
votes
5 answers
How to convert a simple array to an associative array?
What is the fastest way to convert a simple array to an associative array in PHP so that values can be checked in the isset($array[$value])?
I.e. fastest way to do the following conversion:
$array = array(1, 2, 3, 4, 5);
$assoc = array();
foreach…

user773755
- 453
- 1
- 6
- 11
28
votes
5 answers
Sort an associative array in awk
I have an associative array in awk that gets populated like this:
chr_count[$3]++
When I try to print my chr_counts, I use this:
for (i in chr_count) {
print i,":",chr_count[i];
}
But not surprisingly, the order of i is not sorted in any…

lonestar21
- 1,113
- 2
- 13
- 23
20
votes
5 answers
php associative array key order (not sort)
My array:
$data = array('two' => 2, 'one' => 1, 'three' => 3);
Now, with when I iterate the array, the first value that will come up will probably be
$data['two'] // = 2 @ index[0]
right?
What if I want to move the $data[1] to the position of…

nizzle
- 1,036
- 2
- 10
- 23
18
votes
3 answers
python sqlalchemy get column names dynamically?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
connection = create_engine('mysql://user:passwd@localhost:3306/db').connect()
result = connection.execute("select * from table")
for v in result:
print…

ZiTAL
- 3,466
- 8
- 35
- 50
16
votes
4 answers
PDO associative arrays - return associative
I have this code:
$dbInstance = DB_Instance::getDBO();
$statement = $dbInstance->prepare("SELECT id, name FROM language ORDER BY id");
$statement->execute();
$rows = $statement->fetchAll();
//Create associative array wuth id set as an index…

bestprogrammerintheworld
- 5,417
- 7
- 43
- 72
14
votes
1 answer
bash4 read file into associative array
I am able to read file into a regular array with a single statement:
local -a ary
readarray -t ary < $fileName
Not happening is reading a file into assoc. array.
I have control over file creation and so would like to do as simply as possible w/o…

mono-dr
- 217
- 1
- 2
- 9