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 }

 

相关文章:

  • 2021-10-30
  • 2021-10-30
  • 2021-11-01
  • 2021-07-25
  • 2022-12-23
  • 2021-05-24
  • 2021-07-20
  • 2021-08-17
猜你喜欢
  • 2022-12-23
  • 2021-08-03
  • 2021-12-28
  • 2022-02-23
  • 2021-05-31
  • 2021-07-13
  • 2021-09-05
相关资源
相似解决方案