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);
由于偶也是初学,请见谅~~,先挑软柿子捏,看看最简单的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://blog.51cto.com/7814265/1793465