Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences of Array and Hash in PHP?

What are the differences of an Array and a Hash PHP?

An array: array(1,2,3...)
A hash: array(key1=value1, key2=value2, ...)

are they different or the same?

※ For example, will the function arguments allows array be effective for the hash?

Because I distinguish it by the conventional language and used it, I am puzzled.

like image 505
freddiefujiwara Avatar asked May 20 '09 01:05

freddiefujiwara


People also ask

What is the difference between hash and array?

With arrays, the key is an integer, whereas hashes support any object as a key. Both arrays and hashes grow as needed to hold new elements. It's more efficient to access array elements, but hashes provide more flexibility.

What is difference between keyed array and hashed array in data structure?

A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.

What is an array in PHP?

Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data.

What type of an array is equivalent to a hash table in PHP?

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


3 Answers

Both the things you are describing are arrays. The only difference between the two is that you are explicitly setting the keys for the second one, and as such they are known as associative arrays. I do not know where you got the Hash terminology from (Perl?) but that's not what they are referred as in PHP.

So, for example, if you were to do this:

$foo = array(1,2,3,4,5);
print_r($foo);

The output would be:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

As you can see, the keys to access the individual values you put in were created for you, but are there nonetheless. So this array is, essentially, associative as well. The other "type" of array is exactly the same way, except you are explcitily saying "I want to access this value with this key" instead of automatic numeric indexes (although the key you provide could also be numeric).

$bar = array('uno' => 'one', 'dos' => 'two');
print_r($bar);

Would output:

Array
(
    [uno] => one
    [dos] => two
)

As you might then expect, doing print $bar['one'] would output uno, and doing $foo[0] from the first example would output 1.

As far as functions go, PHP functions will most of the time take either one of these "types" of array and do what you want them to, but there are distinctions to be aware of, as some functions will do funky stuff to your indexes and some won't. It is usually best to read the documentation before using an array function, as it will note what the output will be depending on the keys of the array.

You should read the manual for more information.

like image 53
Paolo Bergantino Avatar answered Oct 01 '22 15:10

Paolo Bergantino


In actuality, there are no arrays in php - there are only associative arrays (which is basically a hash table)

Try doing

$ar=array("zero","one","two","three","four");
unset($ar[3]);

doing so will remove "three" from the array, but you'll notice that the array keys (the array is not associative) will remain the same (0,1,2,4) - in any normal language it would renumber the key for "four" to 3.

like image 24
strubester Avatar answered Oct 03 '22 15:10

strubester


Into the engine php all arrays (associative or sequential ) are hash tables, and this because it is the fastest method in the reading of the single element. Internally there are basic functions to create and popolate an array:

int zend_hash_init(HashTable *ht, uint nSize,hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent);

int zend_hash_add(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_update(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_index_update(HashTable *ht, ulong h, void *pDate, uinit nDataSize, void **pDest)

int zend_hash_next_index_insert(HashTable *ht, void *pData, uinit nDataSize, void **pDest)

int zend_hash_add(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_update(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_index_update(HashTable *ht, ulong h, void *pDate, uinit nDataSize, void **pDest)

int zend_hash_next_index_insert(HashTable *ht, void *pData, uinit nDataSize, void **pDest)

......

like image 44
Davide Chiappetta Avatar answered Oct 02 '22 15:10

Davide Chiappetta