分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

php内置函数分析array_diff()

发布时间:2023-09-06 01:58责任编辑:蔡小小关键词:暂无标签
 1 PHP_FUNCTION(array_diff) 2 { 3 ????zval *args; 4 ????int argc, i; 5 ????uint32_t num; 6 ????HashTable exclude; 7 ????zval *value; 8 ????zend_string *str, *key; 9 ????zend_long idx;10 ????zval dummy;11 12 ????// 至少两个参数13 ????if (ZEND_NUM_ARGS() < 2) {14 ????????php_error_docref(NULL, E_WARNING, "at least 2 parameters are required, %d given", ZEND_NUM_ARGS());15 ????????return;16 ????}17 ????// 类型描述符:* ?variable arguments list (0 or more)18 ????// 类型描述符:+ ?variable arguments list (1 or more)19 ????// 参数存放在数组args中,argc保存参数个数20 ????if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) {21 ????????return;22 ????}23 24 ????// 第一个参数不是数组,则报错25 ????if (Z_TYPE(args[0]) != IS_ARRAY) {26 ????????php_error_docref(NULL, E_WARNING, "Argument #1 is not an array");27 ????????RETURN_NULL(); // 返回NULL28 ????}29 30 ????/* count number of elements */31 ????// 检查所有参数是否是数组32 ????num = 0;33 ????for (i = 1; i < argc; i++) {34 ????????if (Z_TYPE(args[i]) != IS_ARRAY) {35 ????????????php_error_docref(NULL, E_WARNING, "Argument #%d is not an array", i + 1);36 ????????????RETURN_NULL(); // 返回NULL37 ????????}38 ????????num += zend_hash_num_elements(Z_ARRVAL(args[i])); // 所有参数数组的元素个数累加39 ????}40 41 ????// 元素个数num为0,返回第一个参数数组(空数组)42 ????if (num == 0) {43 ????????ZVAL_COPY(return_value, &args[0]);44 ????????return;45 ????}46 47 ????ZVAL_NULL(&dummy);48 ????/* create exclude map */49 ????zend_hash_init(&exclude, num, NULL, NULL, 0);50 ????// 从第二个数组开始,所以数组元素保存到exclude51 ????for (i = 1; i < argc; i++) {52 ????????// 遍历数组53 ????????ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL(args[i]), value) {54 ????????????str = zval_get_string(value); // 数组元素值转为字符串55 ????????????zend_hash_add(&exclude, str, &dummy); // 56 ????????????zend_string_release(str);57 ????????} ZEND_HASH_FOREACH_END();58 ????}59 60 ????/* copy all elements of first array that are not in exclude set */61 ????// 初始化返回值数组,大小和第一个数组一致。62 ????array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL(args[0])));63 ????// 循环遍历第一个数组64 ????ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL(args[0]), idx, key, value) {65 ????????str = zval_get_string(value); // 元素值转为字符串66 ????????if (!zend_hash_exists(&exclude, str)) { // exclude中找不到该值67 ????????????// 插入到返回值数组中(差集),键名保留不变。68 ????????????if (key) {69 ????????????????value = zend_hash_add_new(Z_ARRVAL_P(return_value), key, value);70 ????????????} else {71 ????????????????value = zend_hash_index_add_new(Z_ARRVAL_P(return_value), idx, value);72 ????????????}73 ????????????zval_add_ref(value);74 ????????}75 ????????zend_string_release(str);76 ????} ZEND_HASH_FOREACH_END();77 78 ????zend_hash_destroy(&exclude);79 }

php内置函数分析array_diff()

原文地址:https://www.cnblogs.com/natian-ws/p/9151957.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved