This article mainly introduces about the PHP source four: About the Count function, has a certain reference value, now share to everyone, the need for friends can refer to
The Count function is often seen in some interviews or exams, so a glimpse
For count handling of non-arrays
In its code, you can see
Php_function (count) {Zval *array; Long mode = Count_normal; if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "Z|l", &array, &mode) = = FAILURE) return; Switch (z_type_p (array)) {case IS_NULL://Null value processing Return_long (0); Break Case Is_array://processing array, including It is recursive Return_long (php_count_recursive (array, mode TSRMLS_CC)); Break Case Is_object: {#ifdef HAVE_SPL/* It the OBJECT implements countable we call its count () method */ Zval *retval; if (z_obj_ht_p (array)->get_class_entry && instanceof_function (z_objce_p (array), spl_ce_countable Tsrmls_ CC) {zend_call_method_with_0_params (&array, NULL, NULL, "Count", &retval); if (retval) {convert_to_long_ex (&retval); Retval_long (Z_lval_p (RETVAL)); Zval_ptr_dtor (&retval); } Return } #endif/* If not we return the number of properties (not taking visibility to account) */if (z_o Bj_ht_p (array)->count_elements) {Retval_long (1); if (SUCCESS = = Z_obj_ht (*array)->count_elements (array, &z_lval_p (return_value) tsrmls_cc)) {RET Urn }}} Default://Other situations, such as return_long (1), such as strings commonly found in exams; Break }}
It is stated in the manual:
For an object, this function can invoke count () by implementing the countable interface if SPL is installed. The interface has only one method count (), and this method returns the return value of the count () function.
For statistics on the length of the array, if mode uses the default value (0), only the length of the first-dimension array is displayed
The code as shown below
$arr = Array (1, 2, 3); $arr 2 = Array ($arr, $arr); echo Count ($arr 2), ' <br/> '; echo Count ($arr 2, 1); /* Output Result: 28*/
If you just display the first-dimensional array, return directly to the Nnumofelements property of the Hashtable that holds the array.
The code that implements it is:
php_count_recursive function Array.c 251 Line cnt = zend_hash_num_elements (z_arrval_p (array)); Zend_api int zend_hash_num_elements (HashTable *ht) { is_consistent (HT); return ht->nnumofelements;}
If you want to run Hashtable simple operation, punch the PHP source Hashtable simple Example
If you want to see how the array is stored or traversed, bash the bird's deep understanding of PHP's Array (traversal order)
If recursion is initiated, use COUNT ($arr, 1)
The program recursively calls the Php_count_recursive function
For Hashtable, the program traverses the doubly linked list that contains this one-dimensional array, and then recursively iterates through each node's stored pdata, really to traverse through all the nodes
Feel
Hashtable is very powerful, if you want to calculate the length of the array or call the Count function, it would be a thankless thing to invent the wheel again!
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!