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
13
votes
3 answers
JavaScript "associative" array access
I have a simple simulated array with two elements:
bowl["fruit"] = "apple";
bowl["nuts"] = "brazilian";
I can access the value with an event like this:
onclick = "testButton00_('fruit')">with `testButton00_`
function testButton00_(key){
var t…

cp.
- 1,241
- 5
- 15
- 26
12
votes
5 answers
Adding multiple values to an associative array in PHP in one call
I am trying instantiate an associative array and then in a second call, assign it various other value sets on one block-line. I would like to do this following the same form as in the instantiation:
"variable" = > 'value';
My instantiation…

ViviDVasT
- 131
- 1
- 1
- 6
12
votes
5 answers
Which mapping type to choose for associative Arrays? Doctrine ODM
I have a simple question about the (by the way really great!) Doctrine ODM.
Assume you have a document like:
/**
* @Document
*/
class Test
{
/** @Id */
public $id;
/** @WHICHTYPE */
public $field = array();
}
Now i want to store…

Andreas Schmidt
- 121
- 1
- 3
11
votes
2 answers
Accessing associative arrays in PHP
I want to access the index 'memo' in the associative array in PHP below
$variables["thelistitems"];
print_r($variables["thelistitems"]);
Output
Array
(
[0] => Array
(
[productid] => prod:c6dbdd62-dc13-6421-5a94-c8cd871a59d3
…

slao
- 1,986
- 7
- 27
- 37
11
votes
1 answer
Bash reading from a file to an associative array
I'm trying to write a script in bash using an associative array.
I have a file called data:
a,b,c,d,e,f
g,h,i,j,k,l
The following script:
oldIFS=${IFS}
IFS=","
declare -A assoc
while read -a array
do
assoc["${array[0]}"]="${array[@]}"
done
for…

user3598639
- 111
- 1
- 3
10
votes
3 answers
Create an associative array in php with dynamic key and value
I want to create an associative array in php with dynamic key and also a dynamic value from a particular mysql table.
The table name is monthly_salary with a two column named month and salary respectively.
I get the data inside it:
$sql =…

scireon
- 365
- 2
- 4
- 17
9
votes
5 answers
How to filter an associative arrays using array of keys in PHP?
I have an associative arrays and an array of keys.
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
How I build an associative array from all element of $A where the key is in $B?
For the example above, the answer should…

Lhuqita Fazry
- 291
- 4
- 12
9
votes
1 answer
Swift: associative array with multiple keys:values
I am not expert in Swift and I have been using it for few months to build Mac Apps. I would like to represent in memory a data structure like that of PHP associative arrays but in Swift. Let's imagine that I have a table of data to load in memory…

Alessio
- 93
- 1
- 1
- 4
9
votes
3 answers
Check if associative array contains value, and retrieve key / position in array
I'm struggling to explain what I want to do here so apologies if I confuse you.. I'm just as confused myself
I have an array like so:
$foo = array(
array('value' => 5680, 'text' => 'Red'),
array('value' => 7899, 'text' => 'Green'),
…
user1043646
9
votes
5 answers
How can I convert a PHP function's parameter list to an associative array?
I want to convert the arguments to a function into an associative array with keys equal to the parameter variable names, and values equal to the parameter values.
PHP:
function my_function($a, $b, $c) {
// <--- magic goes here to create the…

Fragsworth
- 33,919
- 27
- 84
- 97
9
votes
3 answers
Compare two Arrays Javascript - Associative
I have searched on here for a quality method to compare associative arrays in javascript. The only decent solution I have found is the PHP.JS project which has some comparative array functions. The only problem is that these functions consider the…
user135998
8
votes
5 answers
php - associative array index naming conventions
In PHP, do associative array indexes need to follow that same rules and variable names (can't start with a number, etc.) I am looking for both working and philosophical answers to this question.

user897363
- 81
- 1
- 3
8
votes
2 answers
Why does function composition compose from right to left in Javascript?
Function composition composes from right to left:
const comp = f => g => x => f(g(x));
const inc = x => x + 1;
const dec = x => x - 1;
const sqr = x => x * x;
let seq = comp(dec)(comp(sqr)(inc));
seq(2); // 8
seq(2) is transformed to…
user6445533
8
votes
2 answers
How do you print an associative array in DTrace?
The question pretty much sums it up. "dtrace 'print an associative array'" has exactly one google hit and the similar searches are equally useless.
EDIT:
If I were to use an aggregation, I'm not aware that I'd still be able to remove entries. My…

Sniggerfardimungus
- 11,583
- 10
- 52
- 97
8
votes
4 answers
Associative array in Delphi , array with string key is possible?
If you work with php you can see the php have associative array (or array width string key) in programing lang.
For example:
$server['hostname'] = 'localhost';
$server['database'] = 'test';
$server['username'] = 'root';
$server['password'] = …

A1Gard
- 4,070
- 4
- 31
- 55