【PHP开源代码栏目提醒】:网学会员鉴于大家对PHP开源代码十分关注,论文会员在此为大家搜集整理了“PHP源码之standard模块中array内置函数之初步理解 - 其它资料”一文,供大家参考学习
PHP 源码之 standard 模块中 array 内置函数之初步理解 php 内置了许多处理 array 的函数,这些函数功能简单,但组合使用能够形成强大的功能,非常好用 在 php 源 代 码 中 , array 相 关 的 内 置 函 数 定 义 在 /ext/standard/php_array.h 和/ext/standard/array.c 中,主要的提供的函数如下:PHP_FUNCTIONksortPHP_FUNCTIONkrsortPHP_FUNCTIONnatsortPHP_FUNCTIONnatcasesortPHP_FUNCTIONasortPHP_FUNCTIONarsortPHP_FUNCTIONsortPHP_FUNCTIONrsortPHP_FUNCTIONusortPHP_FUNCTIONuasortPHP_FUNCTIONuksortPHP_FUNCTIONarray_walkPHP_FUNCTIONarray_walk_recursivePHP_FUNCTIONcountPHP_FUNCTIONendPHP_FUNCTIONprevPHP_FUNCTIONnextPHP_FUNCTIONresetPHP_FUNCTIONcurrentPHP_FUNCTIONkeyPHP_FUNCTIONminPHP_FUNCTIONmaxPHP_FUNCTIONin_arrayPHP_FUNCTIONarray_searchPHP_FUNCTIONextractPHP_FUNCTIONcompactPHP_FUNCTIONarray_fillPHP_FUNCTIONarray_fill_keysPHP_FUNCTIONrangePHP_FUNCTIONshufflePHP_FUNCTIONarray_multisortPHP_FUNCTIONarray_pushPHP_FUNCTIONarray_popPHP_FUNCTIONarray_shiftPHP_FUNCTIONarray_unshiftPHP_FUNCTIONarray_splicePHP_FUNCTIONarray_slicePHP_FUNCTIONarray_mergePHP_FUNCTIONarray_merge_recursivePHP_FUNCTIONarray_replacePHP_FUNCTIONarray_replace_recursivePHP_FUNCTIONarray_keysPHP_FUNCTIONarray_valuesPHP_FUNCTIONarray_count_valuesPHP_FUNCTIONarray_reversePHP_FUNCTIONarray_reducePHP_FUNCTIONarray_padPHP_FUNCTIONarray_flipPHP_FUNCTIONarray_change_key_casePHP_FUNCTIONarray_randPHP_FUNCTIONarray_uniquePHP_FUNCTIONarray_intersectPHP_FUNCTIONarray_intersect_keyPHP_FUNCTIONarray_intersect_ukeyPHP_FUNCTIONarray_uintersectPHP_FUNCTIONarray_intersect_assocPHP_FUNCTIONarray_uintersect_assocPHP_FUNCTIONarray_intersect_uassocPHP_FUNCTIONarray_uintersect_uassocPHP_FUNCTIONarray_diffPHP_FUNCTIONarray_diff_keyPHP_FUNCTIONarray_diff_ukeyPHP_FUNCTIONarray_udiffPHP_FUNCTIONarray_diff_assocPHP_FUNCTIONarray_udiff_assocPHP_FUNCTIONarray_diff_uassocPHP_FUNCTIONarray_udiff_uassocPHP_FUNCTIONarray_sumPHP_FUNCTIONarray_productPHP_FUNCTIONarray_filterPHP_FUNCTIONarray_mapPHP_FUNCTIONarray_key_existsPHP_FUNCTIONarray_chunkPHP_FUNCTIONarray_combine由于偶也是初学,请见谅,先挑软柿子捏,看看最简单的 count 函数的实现我们知道 PHP 中提供的 count 函数的形式是 countmixed var int mode,后面这个参数表示是否 recursive True 表示递归计算,否则则只算第一层, ‘b’array‘c’22而 countarray‘a’’1′,比方说 countarray‘a’’1′,‘b’array‘c’2,true3PHP_FUNCTIONcount zval array long mode COUNT_NORMAL //解析参数zl 的形式表示第二个参数可选,PHP 内部默认为 COUNT_NORMAL if zend_parse_parametersZEND_NUM_ARGS TSRMLS_CC zl array mode FAILURE return switch Z_TYPE_Parray case IS_NULL: //countNULL这种直接返回 1 RETURN_LONG0 break case IS_ARRAY: //countarray...会递归计算 RETURN_LONG php_count_recursive array mode TSRMLS_CC break case IS_OBJECT: ifdef HAVE_SPL zval retvalendif / first we check if the handler is defined / //这里没看明白后面补上 if Z_OBJ_HT_Parray-count_elements RETVAL_LONG1 if SUCCESS Z_OBJ_HTarray-count_elementsarrayZ_LVAL_Preturn_value TSRMLS_CC return ifdef HAVE_SPL //countObj //如果 obj 实现了 SPL 中的 Countable 接口,即实现了 count 函数,则会调用这个函数,并用这个函数的输出作为最终的 count 的返回值 / class Test implements Countable public function count return 5 testnew Test 那么 counttest5 / / if not and the object implements Countable we call its count method / if Z_OBJ_HT_Parray-get_class_entry instanceof_functionZ_OBJCE_Parray spl_ce_Countable TSRMLS_CC zend_call_method_with_0_paramsarray NULL NULL count retval if retval convert_to_long_exretval RETVAL_LONGZ_LVAL_Pretval zval_ptr_dtorretval return endif default: //其他的一概返回 1,比方说 //counthello world1 //count1231 等等 RETURN_LONG1 break 未经作者同意不得转载。