数字索引数组根据指定key转关联数组

array.c

PHP_FUNCTION(array_to_associative) {

    zval *array;

    char *key_name;

    uint key_len;

    HashPosition pos;

    zval **operand;

    char *string_key;

    uint string_key_len;

 

    zval **entry;

    uint data_type;

    ulong num_key;

    HashPosition data_pos;

    zval **data;

 

    zend_fcall_info fci = empty_fcall_info;

    zend_fcall_info_cache fci_cache = empty_fcall_info_cache;

    zend_bool have_callback = 0;

    zval *retval = NULL;

 

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "as|f", &array, &key_name, &key_len, &fci, &fci_cache) == FAILURE) {

        return;

    }

    array_init(return_value);

    /*

                    array_init(retval);

     */

 

    if (ZEND_NUM_ARGS() > 2) {

        have_callback = 1;

        fci.no_separation = 0;

        fci.retval_ptr_ptr = &retval;

        fci.param_count = 1;

    }

    for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos);

            zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **) &operand, &pos) == SUCCESS;

            zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos)

            ) {

 

        zval_add_ref(operand);

 

        if (Z_TYPE_PP(operand) != IS_ARRAY) {

            php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred the second dimensional must be array!");

            return;

        }

        /*******callback*******/

        if (have_callback == 1) {

            fci.params = &operand;

            if (zend_call_function(&fci, &fci_cache TSRMLS_CC) != SUCCESS || !retval) {

                php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the associative callback");

                RETURN_NULL();

            }

 

        } else {

            retval = *operand;

        }

 

        for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(*operand), &data_pos);

                zend_hash_get_current_data_ex(Z_ARRVAL_P(*operand), (void **) &entry, &data_pos) == SUCCESS;

                zend_hash_move_forward_ex(Z_ARRVAL_P(*operand), &data_pos)

                ) {

            zval_add_ref(entry);

 

            zend_hash_get_current_key_ex(Z_ARRVAL_P(*operand), &string_key, &string_key_len, &num_key, 0, &data_pos);

            /*

                        php_printf("%s/n", string_key);

             */

 

            zend_hash_get_current_data_ex(Z_ARRVAL_P(*entry), (void **) &data, &data_pos);

            data_type = Z_TYPE_PP(data);

            /*

                        php_printf("%d/n", data_type);

             */

 

            if (strcmp(string_key, key_name) == 0) {

                switch (data_type) {

                    case IS_STRING:

                        /*

                                                php_printf("%s/n", Z_STRVAL_PP(data));

                         */

                        zend_hash_update(Z_ARRVAL_P(return_value), Z_STRVAL_PP(data), Z_STRLEN_PP(data) + 1, &retval, sizeof (zval *), NULL);

                        break;

                    case IS_LONG:

                        /*

                                                php_printf("%d/n", Z_LVAL_PP(data));

                         */

                        zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_PP(data), &retval, sizeof (zval *), NULL);

                        break;

                    case IS_DOUBLE:

                        /*

                                                php_printf("%5.5f/n", Z_DVAL_PP(data));

                         */

                        zend_hash_index_update(Z_ARRVAL_P(return_value), Z_DVAL_PP(data), &retval, sizeof (zval *), NULL);

                        break;

                }

                break;

            }

        }

    }

 

}

basic_functions.c

 

PHP_FE(array_to_associative, arginfo_array_to_associative)

 

ZEND_BEGIN_ARG_INFO(arginfo_array_to_associative, 0)

ZEND_ARG_INFO(0, arg) /* ARRAY_INFO(0, arg, 0) */

ZEND_ARG_INFO(0, key_name)

ZEND_END_ARG_INFO()

 

php_array.h

PHP_FUNCTION(array_to_associative);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值