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.
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.
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.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With