Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/opcache/Optimizer/zend_func_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ static const func_info_t func_infos[] = {
F1("array_pad", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_OF_ANY),
F1("array_flip", MAY_BE_NULL | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_LONG | MAY_BE_ARRAY_OF_STRING),
F1("array_change_key_case", MAY_BE_NULL | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_OF_ANY),
F1("array_change_keys", MAY_BE_NULL | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_OF_ANY),
F1("array_rand", UNKNOWN_INFO),
F1("array_unique", MAY_BE_NULL | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_OF_ANY),
F1("array_intersect", MAY_BE_NULL | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_OF_ANY),
Expand Down
59 changes: 59 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3776,6 +3776,65 @@ PHP_FUNCTION(array_change_key_case)
}
/* }}} */

/** {{{ proto array array_change_keys(array input, mixed callback)
Retuns an array with all keys modified by a callback */
PHP_FUNCTION(array_change_keys)
{
zval *array, *value, *params;
zval result;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
zend_ulong num_key;
zend_string *str_key;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "af", &array, &fci, &fci_cache) == FAILURE) {
return;
}

array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(array)));
params = (zval *)safe_emalloc(2, sizeof(zval), 0);
Copy link
Member

@laruence laruence May 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the size is fixed, you could use stack allocated zval[2]


ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, str_key, value) {
fci.retval = &result;
fci.param_count = 2;
fci.params = params;
fci.no_separation = 0;

if (str_key) {
ZVAL_STR_COPY(&params[0], str_key);
} else {
ZVAL_LONG(&params[0], num_key);
}
ZVAL_COPY(&params[1], value);

if (zend_call_function(&fci, &fci_cache) != SUCCESS || Z_TYPE(result) == IS_UNDEF) {
zval_dtor(return_value);
zval_dtor(&params[0]);
zval_dtor(&params[1]);
efree(params);
RETURN_NULL();
}

zval_dtor(&params[0]);
zval_dtor(&params[1]);

if (Z_TYPE(result) == IS_STRING) {
value = zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR(result), value);
zval_add_ref(value);
Copy link
Member

@laruence laruence May 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nervermind.

} else if (Z_TYPE(result) == IS_LONG) {
value = zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL(result), value);
zval_add_ref(value);
} else {
php_error_docref(NULL, E_WARNING, "New key should be either a string or an integer");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'd better also tell the user which key is illegal.

}

zval_ptr_dtor(&result);
} ZEND_HASH_FOREACH_END();

efree(params);
}
/* }}} */

struct bucketindex {
Bucket b;
unsigned int i;
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_array_change_key_case, 0, 0, 1)
ZEND_ARG_INFO(0, case)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_array_change_keys, 0, 0, 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the required num arg should be 2 here

ZEND_ARG_INFO(0, input) /* ARRAY_INFO(0, arg, 0) */
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_array_unique, 0, 0, 1)
ZEND_ARG_INFO(0, arg) /* ARRAY_INFO(0, arg, 0) */
ZEND_ARG_INFO(0, flags)
Expand Down Expand Up @@ -3312,6 +3317,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(array_pad, arginfo_array_pad)
PHP_FE(array_flip, arginfo_array_flip)
PHP_FE(array_change_key_case, arginfo_array_change_key_case)
PHP_FE(array_change_keys, arginfo_array_change_keys)
PHP_FE(array_rand, arginfo_array_rand)
PHP_FE(array_unique, arginfo_array_unique)
PHP_FE(array_intersect, arginfo_array_intersect)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ PHP_FUNCTION(array_reduce);
PHP_FUNCTION(array_pad);
PHP_FUNCTION(array_flip);
PHP_FUNCTION(array_change_key_case);
PHP_FUNCTION(array_change_keys);
PHP_FUNCTION(array_rand);
PHP_FUNCTION(array_unique);
PHP_FUNCTION(array_intersect);
Expand Down
132 changes: 132 additions & 0 deletions ext/standard/tests/array/array_change_keys.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
--TEST--
Test array_change_keys() function
--FILE--
<?php
$arrayWithIntKeys = ['foo', 'bar'];
$arrayWithStringKeys = ['foo' => 42, 'bar' => 3];
$arrayWithMixedKeys = ['foo' => 42, 'bar'];

echo "---Testing an empty array---\n";
var_dump(array_change_keys([], function(){}));

echo "---Numeric keys to numeric keys---\n";
var_dump(array_change_keys($arrayWithIntKeys, function($k, $v) {
return $k * 10;
}));

echo "---Numeric keys to string keys---\n";
var_dump(array_change_keys($arrayWithIntKeys, function($k, $v) {
return 'test:' . $k;
}));

echo "---String keys to numeric keys---\n";
$i = 0;
var_dump(array_change_keys($arrayWithIntKeys, function($k, $v) use (&$i) {
return $i++;
}));

echo "---String keys to string keys---\n";
var_dump(array_change_keys($arrayWithStringKeys, function($k, $v) {
return 'test:' . $k;
}));

echo "---Mixed keys to numeric keys---\n";
$i = 10;
var_dump(array_change_keys($arrayWithMixedKeys, function($k, $v) use (&$i) {
return $i++;
}));

echo "---Mixed keys to string keys---\n";
var_dump(array_change_keys($arrayWithMixedKeys, function($k, $v) {
return 'test:' . $k;
}));

echo "---Mixed keys to mixed keys---\n";
var_dump(array_change_keys($arrayWithMixedKeys, function($k, $v) {
return is_int($k) ? 'baz' : 3;
}));

echo "---Test using a string as the callable---\n";
// We can't reference 'md5' directly because it would get a truthy value in its second param,
// This resulting in some raw bytes instead of a nice human-readable string.
function use_md5_hash_as_key($k, $v) {
return md5($k);
}
var_dump(array_change_keys($arrayWithMixedKeys, 'use_md5_hash_as_key'));

echo "---Test using the [\$obj, 'method'] callback syntax\n";
$keyMaker = new class {
function getNewKey($k, $v) {
return md5($v);
}
};
var_dump(array_change_keys($arrayWithMixedKeys, [$keyMaker, 'getNewKey']));

?>
--EXPECTF--
---Testing an empty array---
array(0) {
}
---Numeric keys to numeric keys---
array(2) {
[0]=>
string(3) "foo"
[10]=>
string(3) "bar"
}
---Numeric keys to string keys---
array(2) {
["test:0"]=>
string(3) "foo"
["test:1"]=>
string(3) "bar"
}
---String keys to numeric keys---
array(2) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
}
---String keys to string keys---
array(2) {
["test:foo"]=>
int(42)
["test:bar"]=>
int(3)
}
---Mixed keys to numeric keys---
array(2) {
[10]=>
int(42)
[11]=>
string(3) "bar"
}
---Mixed keys to string keys---
array(2) {
["test:foo"]=>
int(42)
["test:0"]=>
string(3) "bar"
}
---Mixed keys to mixed keys---
array(2) {
[3]=>
int(42)
["baz"]=>
string(3) "bar"
}
---Test using a string as the callable---
array(2) {
["acbd18db4cc2f85cedef654fccc4a4d8"]=>
int(42)
["cfcd208495d565ef66e7dff9f98764da"]=>
string(3) "bar"
}
---Test using the [$obj, 'method'] callback syntax
array(2) {
["a1d0c6e83f027327d8461063f4ac58a6"]=>
int(42)
["37b51d194a7513e45b56f6524f2d51f2"]=>
string(3) "bar"
}
27 changes: 27 additions & 0 deletions ext/standard/tests/array/array_change_keys_variation1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Test array_change_keys() function with duplicate keys
--FILE--
<?php

var_dump(array_change_keys([1, 2, 3], function(){ return 'foo'; }));

var_dump(array_change_keys([1, 2, 3], function(){ return 99; }));

var_dump(array_change_keys(range(1, 10), function($k, $v){ return $k % 2 ? 'foo' : 99; }));

?>
--EXPECTF--
array(1) {
["foo"]=>
int(3)
}
array(1) {
[99]=>
int(3)
}
array(2) {
[99]=>
int(9)
["foo"]=>
int(10)
}
Loading