php内置函数分析array_count_values()
1 PHP_FUNCTION(array_count_values) 2 { 3 zval *input, /* Input array */ 4 *entry, /* An entry in the input array */ 5 *tmp; 6 HashTable *myht; 7 8 if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &input) == FAILURE) { 9 return; 10 } 11 12 /* Initialize return array */ 13 array_init(return_value); 14 15 /* Go through input array and add values to the return array */ 16 myht = Z_ARRVAL_P(input); 17 // 循环遍历数组 18 ZEND_HASH_FOREACH_VAL(myht, entry) { 19 ZVAL_DEREF(entry); 20 /* 数组元素的值只能是字符串或整数 */ 21 if (Z_TYPE_P(entry) == IS_LONG) { // 数组元素值为整数 22 if ((tmp = zend_hash_index_find(Z_ARRVAL_P(return_value), Z_LVAL_P(entry))) == NULL) { // 返回数组中不存在该键 23 zval data; 24 ZVAL_LONG(&data, 1); // 首次出现,+1 25 zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(entry), &data); // 添加到返回值数组 26 } else { 27 Z_LVAL_P(tmp)++; // 元素值出现次数+1 28 } 29 } else if (Z_TYPE_P(entry) == IS_STRING) { // 数组元素值为字符串 30 if ((tmp = zend_symtable_find(Z_ARRVAL_P(return_value), Z_STR_P(entry))) == NULL) { 31 zval data; 32 ZVAL_LONG(&data, 1); 33 zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(entry), &data); 34 } else { 35 Z_LVAL_P(tmp)++; 36 } 37 } else { 38 // 数组元素的值非字符串或整数,则报warning错误。 39 php_error_docref(NULL, E_WARNING, "Can only count STRING and INTEGER values!"); 40 } 41 } ZEND_HASH_FOREACH_END(); 42 }