An array of deep PHP cores

Source: Internet
Author: User
Tags php definition

Defined:

The array in PHP is actually an ordered map. A map is a type that associates values to the keys. This type is optimized in many ways, so it can be used as a real array, or as a list (vector), a hash (an implementation of a mapping), a dictionary, a collection, a stack, a queue, and more possibilities. The value of an array element can also be another array. Tree structures and multidimensional arrays are also allowed.

This is the definition of PHP arrays in the manual, essentially a key-value mapping relationship, which is a hash table (hash table).

Here I have to say that the artifact in the PHP kernel is HashTable

Hashtable is the advantage of having a doubly linked list, the variables defined in PHP are stored in a symbol table, and this symbol is actually a hashtable, and each of its elements is a zval*-type variable. In addition, containers that store user-defined functions, classes, resources, and so on are implemented in the kernel in the form of Hashtable.

Therefore, the PHP array read and write can be done in O (1), which is very efficient, so the cost and C + +, Java is Hashtable created

Let's take a look at the PHP definition array

<?php      $array = Array ();      $array ["key"] = "value";  

In the kernel

zval* Array;  Array_init (array);  Add_assoc_string (Array, "key", "value", 1);

That's it.

See how the source code is implemented

In the zend_api.h file, this is a macro definition

#define ARRAY_INIT (ARG)                 _array_init ((ARG), 0 ZEND_FILE_LINE_CC)

Then look at the Zend_api.c file.

/* Argument parsing API--Andrei */zend_api int _array_init (zval *arg, uint size ZEND_FILE_LINE_DC)/* {{*/{alloc_        Hashtable_rel (Z_arrval_p (ARG));        _zend_hash_init (Z_arrval_p (ARG), size, NULL, zval_ptr_dtor, 0 zend_file_line_relay_cc);        Z_type_p (ARG) = Is_array;        return SUCCESS;} /* }}} */

Look again, in the Zend_alloc.h file.

#define ALLOC_HASHTABLE_REL (HT)         = (HASHTABLE *) Emalloc_rel (sizeof (HASHTABLE))

Keep looking.

Zend_api int _zend_hash_init (HashTable *ht, uint nSize, hash_func_t phashfunction, dtor_func_t pdestructor, Zend_bool per  Sistent zend_file_line_dc) {UINT i = 3;//initialization is for 1<<3=8 set_inconsistent (HT_OK);//maximum number is 1>>31 = 2^31  =2147483648 if (nSize >= 0x80000000) {/* Prevent overflow */ht->ntablesize =        0x80000000                } else {while ((1U << i) < nSize) {i++;        } ht->ntablesize = 1 << i;     } ht->ntablemask = 0; /* 0 means that ht->arbuckets is uninitialized */ht->pdestructor = Pdestructor;        /* element's destructor (pointer) */ht->arbuckets = (bucket**) &uninitialized_bucket;  Ht->plisthead = NULL;  /* header element for linear traversal */ht->plisttail = NULL; /* tail element, for linear traversal */ht->nnumofelements = 0; /* Number of actual elements in Hashtable */ht->nnextfreeelement = 0; /* Number index of the next free available location */Ht->pinternalpointer = NULL;        /* Internal position pointer, will be reset, current these traversal functions use */ht->persistent = persistent;        Ht->napplycount = 0;/* Cyclic traversal protection */ht->bapplyprotection = 1; return SUCCESS;}

The above is the array initialization can refer to http://www.laruence.com/2009/08/23/1065.html

Then the process of assigning the value

zval* Val;  Make_std_zval (val);  Zval_string (Val, "value", 0);  Zend_hash_add (H, "key", 4, &val, sizeof (zval*), NULL);  

The kernel provides us with a handy macro to manage arrays

Zend_api int Add_index_long (zval *arg, ulong idx, long N); Zend_api int Add_index_null (zval *arg, ulong idx); Zend_api int Add_index_bool (zval *arg, ulong idx, int b); Zend_api int Add_index_resource (zval *arg, ulong idx, int R); Zend_api int add_index_double (zval *arg, ulong idx, double D); Zend_api int add_index_string (zval *arg, ulong idx, const char *STR, int duplicate); Zend_api int Add_index_stringl (zval *arg, ulong idx, const char *STR, UINT length, int duplicate); Zend_api int Add_index_zval (zval *arg, ULONG Index, Zval *value); Zend_api int Add_next_index_long (Zval *arg, long N); Zend_api int Add_next_index_null (Zval *arg); Zend_api int Add_next_index_bool (zval *arg, int b); Zend_api int Add_next_index_resource (zval *arg, int R); Zend_api int add_next_index_double (Zval *arg, double D); Zend_api int add_next_index_string (zval *arg, const char *STR, int duplicate); Zend_api int Add_next_index_stringl (zval *arg, const char *STR, UINT length, int duplicate); Zend_api int ADD_ASSOC_LONG_EX (Zval *ARG, const char *key, UINT Key_len, long N); Zend_api int add_assoc_null_ex (zval *arg, const char *key, uint key_len); Zend_api int add_assoc_bool_ex (zval *arg, const char *key, uint key_len, int b); Zend_api int add_assoc_resource_ex (zval *arg, const char *key, UINT key_len, int r); Zend_api int add_assoc_double_ex (zval *arg, const char *key, UINT Key_len, double D); Zend_api int add_assoc_string_ex (zval *arg, const char *key, UINT Key_len, char *str, int duplicate); Zend_api int add_assoc_stringl_ex (zval *arg, const char *key, UINT Key_len, char *str, uint length, int duplicate);  Zend_api int add_assoc_zval_ex (zval *arg, const char *key, UINT Key_len, Zval *value), #define Add_assoc_long (__arg, __key, __n) add_assoc_long_ex (__arg, __key, strlen (__key) +1, __n) #define ADD_ASSOC_NULL (__arg, __key) add_assoc_null_ex (__ ARG, __key, strlen (__key) + 1) #define Add_assoc_bool (__arg, __key, __b) add_assoc_bool_ex (__arg, __key, strlen (__key) +1, __b) #define Add_assoc_resource (__arg, __key, __r) ADD_ASSOC_RESOURCE_EX (__arg, __key, strlen (__key) +1, __r) #define Add_assoc_double (__arg, __key, __d) add_assoc_double_ex (__arg , __key, strlen (__key) +1, __d) #define Add_assoc_string (__arg, __key, __str, __duplicate) add_assoc_string_ex (__arg, __ Key, strlen (__key) +1, __STR, __duplicate) #define ADD_ASSOC_STRINGL (__arg, __key, __str, __length, __duplicate) add_ ASSOC_STRINGL_EX (__arg, __key, strlen (__key) +1, __str, __length, __duplicate) #define ADD_ASSOC_ZVAL (__arg, __key, __ Value) add_assoc_zval_ex (__arg, __key, strlen (__key) +1, __value)

Specific implementation

$arr [] = NULL;  Add_next_index_null (arr);  $arr [] =;    Add_next_index_long (arr.);  $arr [] = true;  Add_next_index_bool (arr, 1);  $arr [] = 3.14;  Add_next_index_double (arr, 3.14);  $arr [] = ' foo '; Add_next_index_string (arr, "foo");  $arr [] = $var;  Add_next_index_zval (arr, zval);  $arr [0] = NULL; Add_index_null (arr, 0);  $arr [1] =;       Add_index_long (arr, 1,);  $arr [2] = true;     Add_index_bool (arr, 2, 1);  $arr [3] = 3.14;     Add_index_double (arr, 3, 3.14);  $arr [4] = ' foo ';        Add_index_string (arr, 4, "foo", 1);  $arr [5] = $var;     Add_index_zval (arr, 5, zval); $arr ["abc"] = NULL; Add_assoc_null (arr, "abc");  $arr ["def"] =;   Add_assoc_long (arr, "def",);  $arr ["ghi"] = true; Add_assoc_bool (arr, "Ghi", 1);  $arr ["JKL"]  = 3.14 add_assoc_double (arr, "JKL", 3.14);  $arr ["MnO"]= "foo"   add_assoc_string (arr, "MnO", "foo", 1 ");  $arr ["PQR"] = $var; Add_assoc_zval (arr, "PQR", zval);  

Testing in Extensions

Php_function (array_test1) {zval *value;zval *element;char *s= "This is a test"; Char *key= "a"; Make_std_zval (Element); Make_std_zval (value); Array_init (value); Zval_string (Element,s,strlen (s)); Zend_hash_update (Value->value.ht,key,strlen (Key) +1, (void*) &element, sizeof (zval*), NULL); Zend_set_symbol (EG (active_symbol_table), "Siren", value); }php_function (array_test2) {  char* str;  zval* Subarray;  Array_init (Return_value); Array_init (subarray);  Add_next_index_string (Return_value, "test", 1);  str = Estrdup ("This is a Test");  Add_next_index_string (return_value, str, 0);  Add_assoc_double (Return_value, "double", 3.14);  Add_next_index_string (Subarray, "Hello", 1);  Add_assoc_zval (Return_value, "Xsubarray", Subarray);  }  

An array of deep PHP cores

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.