What are the differences between Array and Hash in PHP?

What are the differences in array and hash php?

Array: array(1,2,3...)
Hash: array(key1=value1, key2=value2, ...)

are they different or the same?

※ For example, will function arguments allow an array for a hash?

Because I distinguish it in ordinary language and use it, I am puzzled.

+5
source share
3 answers

Both things that you describe are arrays. The only difference between the two is that you explicitly set the keys for the second, and as such they are known as associative arrays . I don’t know where you got the hash terminology (Perl?), But that’s not what they refer to as PHP.

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

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

The conclusion will be:

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

As you can see, the keys for accessing the individual values ​​you entered were created for you, but nonetheless exist. So this array is essentially associative. The other "type" of the array is exactly the same, except that you explicitly say "I want to access this value using this key" instead of automatic numeric indices (although the key you provided may also be numeric).

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

Output:

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

As you might expect, executing print $bar['one'] outputs uno , and executing $foo[0] from the first example will result in output 1 .

As for functions, PHP functions in most cases use one of these "types" of the array and do what you want, but there are differences that you need to know about, since some functions will make funky to your indexes, and some not. It is usually best to read the documentation before using the array function, as it will notice which result will depend on the keys of the array.

For more information, you should read the manual .

+15
source

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

Try to do

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

this will remove the “three” from the array, but you will notice that the keys of the array (the array is not associative) will remain unchanged (0,1,2,4) - in any normal language that it renumber the key for “four” to 3.

+1
source

In the php engine, all arrays (associative or sequential) are hash tables, which is because it is the fastest method when reading a single element. Inside there are basic functions for creating and filling 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) 

......

+1
source

All Articles