freddiefujiwara
freddiefujiwara

Reputation: 59049

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.

Upvotes: 6

Views: 7301

Answers (3)

Davide Chiappetta
Davide Chiappetta

Reputation: 11

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)

......

Upvotes: 1

strubester
strubester

Reputation: 285

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.

Upvotes: 1

Paolo Bergantino
Paolo Bergantino

Reputation: 488464

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.

Upvotes: 15

Related Questions