array_count_values源码

PHP_FUNCTION(array_count_values)
{
    zval    *input,            /* Input array */
            **entry,        /* An entry in the input array */
            **tmp;
    HashTable *myht;
    HashPosition pos;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &input) == FAILURE) {
        return;
    }

    /* Initialize return array */
    array_init(return_value);

    /* Go through input array and add values to the return array */
    myht = Z_ARRVAL_P(input);
    zend_hash_internal_pointer_reset_ex(myht, &pos);
    while (zend_hash_get_current_data_ex(myht, (void **)&entry, &pos) == SUCCESS) {
        if (Z_TYPE_PP(entry) == IS_LONG) {
            if (zend_hash_index_find(Z_ARRVAL_P(return_value), Z_LVAL_PP(entry), (void **)&tmp) == FAILURE) {
                zval *data;
                MAKE_STD_ZVAL(data);
                ZVAL_LONG(data, 1);
                zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_PP(entry), &data, sizeof(data), NULL);
            } else {
                Z_LVAL_PP(tmp)++;
            }
        } else if (Z_TYPE_PP(entry) == IS_STRING) {
            if (zend_symtable_find(Z_ARRVAL_P(return_value), Z_STRVAL_PP(entry), Z_STRLEN_PP(entry) + 1, (void**)&tmp) == FAILURE) {
                zval *data;
                MAKE_STD_ZVAL(data);
                ZVAL_LONG(data, 1);
                zend_symtable_update(Z_ARRVAL_P(return_value), Z_STRVAL_PP(entry), Z_STRLEN_PP(entry) + 1, &data, sizeof(data), NULL);
            } else {
                Z_LVAL_PP(tmp)++;
            }
        } else {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only count STRING and INTEGER values!");
        }

        zend_hash_move_forward_ex(myht, &pos);
    }
}

记录该源码,等待后续的分析。

posted @ 2014-03-04 11:58  minbbp  阅读(226)  评论(0编辑  收藏  举报