php中 计算数组个数用哪个内置函数,PHP源码之standard模块中array内置函数之初步理解...

php内置了许多处理array的函数,这些函数功能简单,但组合使用能够形成强大的功能,非常好用~~

在php 源代码中,array相关的内置函数定义在/ext/standard/php_array.h和/ext/standard/array.c中,主要的提供的函数如下:

PHP_FUNCTION(ksort);

PHP_FUNCTION(krsort);

PHP_FUNCTION(natsort);

PHP_FUNCTION(natcasesort);

PHP_FUNCTION(asort);

PHP_FUNCTION(arsort);

PHP_FUNCTION(sort);

PHP_FUNCTION(rsort);

PHP_FUNCTION(usort);

PHP_FUNCTION(uasort);

PHP_FUNCTION(uksort);

PHP_FUNCTION(array_walk);

PHP_FUNCTION(array_walk_recursive);

PHP_FUNCTION(count);

PHP_FUNCTION(end);

PHP_FUNCTION(prev);

PHP_FUNCTION(next);

PHP_FUNCTION(reset);

PHP_FUNCTION(current);

PHP_FUNCTION(key);

PHP_FUNCTION(min);

PHP_FUNCTION(max);

PHP_FUNCTION(in_array);

PHP_FUNCTION(array_search);

PHP_FUNCTION(extract);

PHP_FUNCTION(compact);

PHP_FUNCTION(array_fill);

PHP_FUNCTION(array_fill_keys);

PHP_FUNCTION(range);

PHP_FUNCTION(shuffle);

PHP_FUNCTION(array_multisort);

PHP_FUNCTION(array_push);

PHP_FUNCTION(array_pop);

PHP_FUNCTION(array_shift);

PHP_FUNCTION(array_unshift);

PHP_FUNCTION(array_splice);

PHP_FUNCTION(array_slice);

PHP_FUNCTION(array_merge);

PHP_FUNCTION(array_merge_recursive);

PHP_FUNCTION(array_replace);

PHP_FUNCTION(array_replace_recursive);

PHP_FUNCTION(array_keys);

PHP_FUNCTION(array_values);

PHP_FUNCTION(array_count_values);

PHP_FUNCTION(array_reverse);

PHP_FUNCTION(array_reduce);

PHP_FUNCTION(array_pad);

PHP_FUNCTION(array_flip);

PHP_FUNCTION(array_change_key_case);

PHP_FUNCTION(array_rand);

PHP_FUNCTION(array_unique);

PHP_FUNCTION(array_intersect);

PHP_FUNCTION(array_intersect_key);

PHP_FUNCTION(array_intersect_ukey);

PHP_FUNCTION(array_uintersect);

PHP_FUNCTION(array_intersect_assoc);

PHP_FUNCTION(array_uintersect_assoc);

PHP_FUNCTION(array_intersect_uassoc);

PHP_FUNCTION(array_uintersect_uassoc);

PHP_FUNCTION(array_diff);

PHP_FUNCTION(array_diff_key);

PHP_FUNCTION(array_diff_ukey);

PHP_FUNCTION(array_udiff);

PHP_FUNCTION(array_diff_assoc);

PHP_FUNCTION(array_udiff_assoc);

PHP_FUNCTION(array_diff_uassoc);

PHP_FUNCTION(array_udiff_uassoc);

PHP_FUNCTION(array_sum);

PHP_FUNCTION(array_product);

PHP_FUNCTION(array_filter);

PHP_FUNCTION(array_map);

PHP_FUNCTION(array_key_exists);

PHP_FUNCTION(array_chunk);

PHP_FUNCTION(array_combine);

由于偶也是初学,请见谅~~,先挑软柿子捏,看看最简单的count函数的实现

我们知道PHP中提供的count函数的形式是count(mixed var [, int mode]),后面这个参数表示是否recursive True表示递归计算,否则则只算第一层,

比方说count(array('a'=>'1',‘b'=>array(‘c’=>2))=2;而count(array('a'=>'1',‘b'=>array(‘c’=>2),true)=3

PHP_FUNCTION(count)

{

zval *array;

long mode = COUNT_NORMAL;

//解析参数,z|l的形式表示第二个参数可选,PHP内部默认为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:

//count(NULL)这种直接返回1

RETURN_LONG(0);

break;

case IS_ARRAY:

//count(array(...))会递归计算

RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC));

break;

case IS_OBJECT: {

#ifdef HAVE_SPL

zval *retval;

#endif

/* first, we check if the handler is defined */

//这里没看明白~~后面补上

if (Z_OBJ_HT_P(array)->count_elements) {

RETVAL_LONG(1);

if (SUCCESS == Z_OBJ_HT(*array)->count_elements(array, &Z_LVAL_P(return_value) TSRMLS_CC)) {

return;

}

}

#ifdef HAVE_SPL

//count(Obj)

//如果obj实现了SPL中的Countable接口,即实现了count函数,则会调用这个函数,并用这个函数的输出作为最终的count的返回值

/**

* class Test implements Countable{

* public function count(){

* return 5;

* }

* }

* $test=new Test();

* 那么count($test)=5

*/

/* if not and the object implements Countable we call its count() method */

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

}

default:

//其他的一概返回1,比方说

//count("hello world")=1

//count(123)=1等等

RETURN_LONG(1);

break;

}

}

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3lemmugb3mg4c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值