94

Recently, I have read about hash tables in a very famous book, "Introduction to Algorithms". I haven't used them in any real applications yet, but I want to. But I don't know how to start.
What are some samples of using it, for example, how to realize a dictionary application (like ABBYY Lingvo) using hash tables?

And finally I would like to know what is the difference between hash tables and associative arrays in PHP, I mean which technology should I use and in which situations?

If I am wrong (I beg pardon) please correct me, because actually I am starting with hash tables and I have just basic (theoretical) knowledge about them.

2
  • 1
    see stackoverflow.com/questions/2350361/…
    – Artefacto
    Commented Jun 28, 2010 at 16:53
  • For myself if I'm ever back here. PHP associative arrays are just better than Maps right now. On a benchmark test a Map used nearly twice as much memory as an associative array, and population and retrieval of all records was almost 5 times faster for the associative array.
    – Infineight
    Commented May 10, 2023 at 17:35

5 Answers 5

138

In PHP, associative arrays are implemented as hash tables, with a bit of extra functionality.

However, technically speaking, an associative array is not identical to a hash table; it's simply implemented in part with a hash table behind the scenes. Because most of its implementation is a hash table, it can do everything a hash table can, but it can do more, too.

For example, you can loop through an associative array using a for loop, which you can't do with a hash table.

So while they're similar, an associative array can actually do a superset of what a hash table can do, so they're not exactly the same thing. Think of it as hash tables plus extra functionality.

Code examples:

Using an associative array as a hash table:

$favoriteColor = array();
$favoriteColor['bob'] = 'blue';
$favoriteColor['Peter'] = 'red';
$favoriteColor['Sally'] = 'pink';
echo 'bob likes: ' . $favoriteColor['bob'] . "\n";
echo 'Sally likes: ' . $favoriteColor['Sally'] . "\n";
// Output: bob likes blue
//         Sally likes pink

Looping through an associative array:

$idTable = array();
$idTable['Tyler'] = 1;
$idTable['Bill'] = 20;
$idTable['Marc'] = 4;
// Up until here, we're using the array as a hash table.

// Now we loop through the array - you can't do this with a hash table:
foreach($idTable as $person => $id)
    echo 'id: ' . $id . ' | person: ' . $person . "\n";

// Output: id: 1 | person: Tyler
//         id: 20 | person: Bill
//         id: 4 | person: Marc

Note especially how in the second example, the order of each element is maintained (Tyler, Bill, and Marc) based on the order in which they were entered into the array. This is a major difference between associative arrays and hash tables. A hash table does not maintain any connection between the items it holds, whereas a PHP associative array does (you can even sort a PHP associative array).

11
  • 3
    Hmmm, such a short explanation. So they are ABSOLUTELY the same thing?
    – Bakhtiyor
    Commented Jun 28, 2010 at 16:43
  • 2
    @Bak They aren't in general, but they are in PHP, which plays a bit fast and loose with data structures since there's less of a concern over performance Commented Jun 28, 2010 at 16:45
  • 4
    @Michael you make it sound like a disadvantage? It makes PHP different, but I think it's a good difference.
    – delete me
    Commented Jun 28, 2010 at 16:52
  • 1
    @Bakhityor: Your last sentence is perfect. You don't need to 'forget' about hashtables though - in fact it's great you already understand hashtables, because now you can apply that knowledge to associative arrays. I'm adding some examples to my answer to further clarify thing for you.
    – Cam
    Commented Jun 28, 2010 at 17:12
  • 1
    Good answer. I would like to expand it with the fact that not only the underlying implementation of arrays in PHP are hash tables, they are also used to store almost everything else: objects' properties and methods, functions, variables. etc. Commented Oct 30, 2021 at 9:19
23

PHP arrays are basically hash tables.

3
  • 14
    no way. a hash table would require some sort of collision resolution, which php arrays doesnt have. Their collision resolution strategy is just replacing the old value, and thats not a hash table by definition.
    – ccov77
    Commented Mar 21, 2015 at 13:23
  • 4
    As far as I recall, the collision resolution in hash tables is for the hashed key, not the original key (How should that even work?) Commented Jan 30, 2017 at 15:34
  • per Juan's comment: an array can be implemented as a hash table, but does not mean that it is a hash table. Arrays don't need collision detection so any underlying implementation can ignore collisions (just replace values through assignment).
    – NeoH4x0r
    Commented Feb 6, 2021 at 23:04
21

The difference between an associative array and a hash table is that an associative array is a data type, while a hash table is a data implementation. Obviously the associative array type is very important in many current programming languages: Perl, Python, PHP, etc. A hash table is the main way to implement an associative array, but not quite the only way. And associative arrays are the main use of hash tables, but not quite the only use. So it's not that they are the same, but if you already have associative arrays, then you usually shouldn't worry about the difference.

For performance reasons, it can be important to know that your associative arrays in your favorite language are implemented as hashes. And it can be important to have some idea of the overhead cost of that implementation. Hash tables are slower and use more memory than linear arrays as you see them in C.

Perl lumps the two concepts together by calling associative arrays "hashes". Like a number of features of Perl, it isn't quite wrong, but it's sloppy.

2
  • Could you give an example of associative array not implemented with a hashtable?
    – gberth
    Commented Aug 16, 2022 at 19:59
  • 1
    For instance, you could implement an associative array using one or another incremental sorting algorithm, without using a hash function for the keys. Commented Aug 17, 2022 at 21:50
7

An array in PHP is actually an ordered map, not a hash table.

The main difference between map and hash table consists in inability to remember the order in which elements have been added. On the other hand, hash tables are much faster than maps. The complexity of fetching an element from a map is O(n·logn) and from a hash table it is O(1).

2
  • 5
    "Complexity of fetching an element from map is O(nlogn)" - this is simply not true. Even for a LinkedList, fetching a given element is only O(n). What is more, as addressed at en.wikipedia.org/wiki/Hash_table, the hash table used in PHP to implement an associative array has lookup of O(1)
    – StackG
    Commented Jun 21, 2015 at 11:49
  • 2
    As explained here after checking the source code, associative arrays in PHP are implemented as hash tables where "each value stored in the hash is linked to the value stored before it and the value stored after" as a linked list. So it uses extra memory for that, but accessing a certain element using a key is equally fast as an usual hash table, O(1), not slower. Commented Oct 13, 2019 at 3:26
3

An associative array is an array where you don't access elements by an index, but by a key. How this works internally is implementation specific (there is no rule how it must work). An associative array could be implemented by a hash table (most implementations will do that), but it could also be implemented by some sort of tree structure or a skip list or the algorithm just iterates over all elements in the array and looks for a key that matches (this would be awfully slow, but it works).

A hash table is a way how to store data where values are associated to keys and where you intend to find values for keys within a (usually almost) constant time. This sounds exactly like what you expect of an associative array, that's why most of the time hash tables are used for implementing those arrays, but that is not mandatory.

Not the answer you're looking for? Browse other questions tagged or ask your own question.