: This article mainly introduces an array operation in zend. For more information about PHP tutorials, see. The array value is stored in the zvalue_value.ht field. ht is a HashTable data.
In the Zend/zend_vm_execute.h file
Static int ZEND_FASTCALL ZEND_INIT_ARRAY_SPEC_CV_CONST_HANDLER (ZEND_OPCODE_HANDLER_ARGS)
{
Zend_op * opline = EX (opline );
Array_init (& EX_T (opline-> result. u. var). tmp_var); // allocates array memory space and initializes
If (IS_CV = IS_UNUSED ){
ZEND_VM_NEXT_OPCODE ();
# If 0 | IS_CV! = IS_UNUSED
} Else {
Return ZEND_ADD_ARRAY_ELEMENT_SPEC_CV_CONST_HANDLER (ZEND_OPCODE_HANDLER_ARGS_PASSTHRU );
# Endif}
}
The array initialization function is array_init.
ZEND_API int _ array_init (zval * arg, uint size ZEND_FILE_LINE_DC )/*{{{*/
{
ALLOC_HASHTABLE_REL (Z_ARRVAL_P (arg); // allocate memory
_ Zend_hash_init (Z_ARRVAL_P (arg), size, NULL, ZVAL_PTR_DTOR, 0 ZEND_FILE_LINE_RELAY_CC );
Z_TYPE_P (arg) = IS_ARRAY; // array type
Return SUCCESS;
}
No. Hash table initialization function _ zend_hash_init
In PHP extensions, we can write as follows:
PHP_FUNCTION (test)
{
Zval * value;
MAKE_STD_ZVAL (value );
Array_init (value );
ZEND_SET_SYMBOL (EG (active_symbol_table), "star", value );
Key code for adding an element
Add_assoc_long (zval * array, char * key, long n); add a long integer element.
Add_assoc_unset (zval * array, char * key); add an unset element.
Add_assoc_bool (zval * array, char * key, int B); add a Boolean value.
Add_assoc_resource (zval * array, char * key, int r); add a resource.
Add_assoc_double (zval * array, char * key, double d); add a floating point value.
Add_assoc_string (zval * array, char * key, char * str, int duplicate); add a string. Duplicate indicates whether the string is to be copied to the internal memory of Zend.
Add_assoc_stringl (zval * array, char * key, char * str, uint length, int duplicate); add a string of the specified length.
Add_assoc_zval (zval * array, char * key, zval * value); add a zval structure.
Add_index_long (zval * array, uint idx, long n); add a long integer element.
Add_index_unset (zval * array, uint idx); add an unset element.
Add_index_bool (zval * array, uint idx, int B); add a Boolean value.
Add_index_resource (zval * array, uint idx, int r); add a resource.
Add_index_double (zval * array, uint idx, double d); add a floating point value.
Add_index_string (zval * array, uint idx, char * str, int duplicate );
Add a string. Duplicate indicates whether the string is to be copied to the internal memory of Zend.
Add_index_stringl (zval * array, uint idx, char * str, uint length, int duplicate); add a string of the specified length.
Add_index_zval (zval * array, uint idx, zval * value); add a zval structure.
Add_next_index_long (zval * array, long n); add a long integer element.
Add_next_index_unset (zval * array); add an unset element.
Add_next_index_bool (zval * array, int B); add a Boolean value.
Add_next_index_resource (zval * array, int r); add a resource.
Add_next_index_double (zval * array, double d); add a floating point value.
Add_next_index_string (zval * array, char * str, int duplicate); add a string. Duplicate indicates whether the string is to be copied to the internal memory of Zend.
Add_next_index_stringl (zval * array, char * str, uint length, int duplicate); add a string of the specified length.
Add_next_index_zval (zval * array, zval * value); add a zval structure. Add another array, object, or stream.
The preceding section describes an array operation in zend, including some content. I hope my friends who are interested in the PHP Tutorial will be helpful.