array_change_key_case() 将 array 数组中的所有键名改为全小写或大写。本函数不改变数字索引。
array array_change_key_case ( array
array[,int
case = CASE_LOWER ] )
参数 ¶
array
需要操作的数组。
case
可以在这里用两个常量,CASE_UPPER 或 CASE_LOWER(默认值)。
返回值 ¶
返回一个键全是小写或者全是大写的数组;如果输入值(array)不是一个数组,那么返回FALSE
错误/异常 ¶
如果输入值(array)不是一个数组,就会抛出一个错误警告(E_WARNING)。
范例 ¶
<?php
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>
输出结果:
Array
(
[FIRST] => 1
[SECOND] => 4
)
源码解析:
PHP_FUNCTION(array_change_key_case)
{
zval *array, *entry; //
zend_string *string_key;
zend_string *new_key;
zend_ulong num_key;
zend_long change_to_upper=0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|l", &array, &change_to_upper) == FAILURE) {
return;
}
array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(array)));
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, string_key, entry) {
if (!string_key) {
entry = zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
} else {
if (change_to_upper) {
new_key = php_string_toupper(string_key);
} else {
new_key = php_string_tolower(string_key);
}
entry = zend_hash_update(Z_ARRVAL_P(return_value), new_key, entry);
zend_string_release(new_key);
}
zval_add_ref(entry);
} ZEND_HASH_FOREACH_END();
}
/* }}} */
struct bucketindex {
Bucket b;
unsigned int i;
};
static void array_bucketindex_swap(void *p, void *q) /* {{{ */
{
struct bucketindex *f = (struct bucketindex *)p;
struct bucketindex *g = (struct bucketindex *)q;
struct bucketindex t;
t = *f;
*f = *g;
*g = t;
}
/* }}} */
/* {{{ proto array array_unique(array input [, int sort_flags])
Removes duplicate values from array */
PHP_FUNCTION(array_unique)
{
zval *array;
uint idx;
Bucket *p;
struct bucketindex *arTmp, *cmpdata, *lastkept;
unsigned int i;
zend_long sort_type = PHP_SORT_STRING;
compare_func_t cmp;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|l", &array, &sort_type) == FAILURE) {
return;
}
cmp = php_get_data_compare_func(sort_type, 0);
if (Z_ARRVAL_P(array)->nNumOfElements <= 1) { /* nothing to do */
ZVAL_COPY(return_value, array);
return;
}
RETVAL_ARR(zend_array_dup(Z_ARRVAL_P(array)));
/* create and sort array with pointers to the target_hash buckets */
arTmp = (struct bucketindex *) pemalloc((Z_ARRVAL_P(array)->nNumOfElements + 1) * sizeof(struct bucketindex), Z_ARRVAL_P(array)->u.flags & HASH_FLAG_PERSISTENT);
if (!arTmp) {
zval_dtor(return_value);
RETURN_FALSE;
}
for (i = 0, idx = 0; idx < Z_ARRVAL_P(array)->nNumUsed; idx++) {
p = Z_ARRVAL_P(array)->arData + idx;
if (Z_TYPE(p->val) == IS_UNDEF) continue;
if (Z_TYPE(p->val) == IS_INDIRECT && Z_TYPE_P(Z_INDIRECT(p->val)) == IS_UNDEF) continue;
arTmp[i].b = *p;
arTmp[i].i = i;
i++;
}
ZVAL_UNDEF(&arTmp[i].b.val);
zend_sort((void *) arTmp, i, sizeof(struct bucketindex),
cmp, (swap_func_t)array_bucketindex_swap);
/* go through the sorted array and delete duplicates from the copy */
lastkept = arTmp;
for (cmpdata = arTmp + 1; Z_TYPE(cmpdata->b.val) != IS_UNDEF; cmpdata++) {
if (cmp(lastkept, cmpdata)) {
lastkept = cmpdata;
} else {
if (lastkept->i > cmpdata->i) {
p = &lastkept->b;
lastkept = cmpdata;
} else {
p = &cmpdata->b;
}
if (p->key == NULL) {
zend_hash_index_del(Z_ARRVAL_P(return_value), p->h);
} else {
if (Z_ARRVAL_P(return_value) == &EG(symbol_table)) {
zend_delete_global_variable(p->key);
} else {
zend_hash_del(Z_ARRVAL_P(return_value), p->key);
}
}
}
}
pefree(arTmp, Z_ARRVAL_P(array)->u.flags & HASH_FLAG_PERSISTENT);
}