Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define associative array of arrays

I want to define an associative array like this

var theVar = [   { "100", [0, 1, 2] },   { "101", [3, 4, 5] } ] 

Essentially I want to be able to access an array of three numbers by specifying the custom index.

However, no matter what I try I cannot make it work.

I know I can define it as:

theVar["100"] = [0, 1, 2]; theVar["101"] = [1, 2, 3]; 

But I am setting this somewhere else and I'd prefer to be able to set it in a single statement.

like image 756
Goro Avatar asked May 27 '10 21:05

Goro


People also ask

What is array explain associative array with example?

An array refers to a data structure storing one or more related types of values in a single value. For instance, if you are looking to store 100 numbers, instead of specifying 100 variables, you can simply define an array of length 100.N.

Which is a example of associative array *?

Associative array will have their index as string so that you can establish a strong association between key and values. The associative arrays have names keys that is assigned to them. $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115"); Above, we can see key and value pairs in the array.

What is associative array and multidimensional array?

PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.


1 Answers

theVar = {   "100": [0, 1, 2],   "101": [3, 4, 5] } 

might do the trick. You can then access using theVar["101"] (or theVar[101] for that matter).

(As var is also a keyword in JavaScript, using it as a variable name is very likely to cause problems.)

like image 52
MvanGeest Avatar answered Sep 22 '22 00:09

MvanGeest