Chris_45
Chris_45

Reputation: 9053

Can you say that associative arrays in PHP are like 2D arrays?

Can you say that associative arrays in PHP are like 2D arrays?

Upvotes: 1

Views: 197

Answers (4)

Federico klez Culloca
Federico klez Culloca

Reputation: 27129

From wikipedia Associative array

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).

So an associative array is actually an ADT, implemented in another way. Instead, a 2d array "really" has two dimensions and is, usually, a primitive type.

Upvotes: 0

miku
miku

Reputation: 188114

A 2D array is more like a matrix, a plane, a coordinate system. An associative array on the other hand could be called a dictionary or hash.

Upvotes: 0

ty812
ty812

Reputation: 3323

$var[$x] = 1-dimensional
$var[$y][$y2] = 2-dimensional
$var[$z][$z2][$z3] = 3-dimensional

It doesn't matter if $x, $y or $z are numeric or strings, actually.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361849

No, they are still one-dimensional just like regular 0-based arrays. The difference is that you aren't limited to integers for the keys; you can use any arbitrary string.

And strictly speaking there isn't a technical distinction between associative and non-associative arrays. They use the same syntax, it's just your choice whether you use integers or strings or both for the keys.

Upvotes: 6

Related Questions