php内置函数分析array_sum()

 1 PHP_FUNCTION(array_sum)
 2 {
 3     zval *input, 
 4          *entry,
 5          entry_n;
 6 
 7     if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &input) == FAILURE) {
 8         return;
 9     }
10 
11     // 初始化返回值
12     ZVAL_LONG(return_value, 0);
13 
14     // 循环取数组元素(entry)
15     ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(input), entry) {
16         // 跳过数组和对象
17         if (Z_TYPE_P(entry) == IS_ARRAY || Z_TYPE_P(entry) == IS_OBJECT) {
18             continue;
19         }
20         // 数组元素复制
21         ZVAL_COPY(&entry_n, entry);
22         // 数组元素转为数字
23         convert_scalar_to_number(&entry_n);
24         // 数组元素累加
25         fast_add_function(return_value, return_value, &entry_n);
26     } ZEND_HASH_FOREACH_END();
27 }

猜你喜欢

转载自www.cnblogs.com/natian-ws/p/9103853.html