diff --git a/ext/standard/array.c b/ext/standard/array.c index de2bc15cbfa..567bdc819f8 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1646,6 +1646,58 @@ PHP_FUNCTION(array_fill) /* }}} */ +/* {{{ proto array array_fill_keys(array keys, mixed val) + Create an array using the elements of the first parameter as keys each initialized to val */ +PHP_FUNCTION(array_fill_keys) +{ + zval **keys, **val, **entry; + HashPosition pos; + + if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &keys, &val) == FAILURE) { + WRONG_PARAM_COUNT; + } + + if (Z_TYPE_PP(keys) != IS_ARRAY) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "First parameter must be an array"); + RETURN_FALSE; + } + + /* Initialize return array */ + array_init(return_value); + + if (!zend_hash_num_elements(Z_ARRVAL_PP(keys))) { + return; + } + + if (PZVAL_IS_REF(*val)) { + SEPARATE_ZVAL(val); + } + + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(keys), &pos); + while (zend_hash_get_current_data_ex(Z_ARRVAL_PP(keys), (void **)&entry, &pos) == SUCCESS) { + zval_add_ref(val); + + if (Z_TYPE_PP(entry) == IS_STRING || Z_TYPE_PP(entry) == IS_UNICODE) { + zend_u_symtable_update(Z_ARRVAL_P(return_value), Z_TYPE_PP(entry), Z_UNIVAL_PP(entry), Z_UNILEN_PP(entry) + 1, val, sizeof(zval *), NULL); + } else if (Z_TYPE_PP(entry) == IS_LONG) { + zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_PP(entry), val, sizeof(zval *), NULL); + } else { + zval tmpkey; + + tmpkey = **entry; + zval_copy_ctor(&tmpkey); + convert_to_string(&tmpkey); + + zend_symtable_update(Z_ARRVAL_P(return_value), Z_STRVAL(tmpkey), Z_STRLEN(tmpkey) + 1, val, sizeof(zval *), NULL); + + zval_dtor(&tmpkey); + } + zend_hash_move_forward_ex(Z_ARRVAL_PP(keys), &pos); + } +} +/* }}} */ + + /* {{{ proto array range(mixed low, mixed high[, int step]) U Create an array containing the range of integers or characters from low to high (inclusive) */ PHP_FUNCTION(range) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 5e24535d21c..1aed0ad8638 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -379,6 +379,12 @@ ZEND_BEGIN_ARG_INFO(arginfo_array_fill, 0) ZEND_ARG_INFO(0, val) ZEND_END_ARG_INFO() +static +ZEND_BEGIN_ARG_INFO(arginfo_array_fill_keys, 0) + ZEND_ARG_INFO(0, keys) /* ARRAY_INFO(0, keys, 0) */ + ZEND_ARG_INFO(0, val) +ZEND_END_ARG_INFO() + static ZEND_BEGIN_ARG_INFO_EX(arginfo_range, 0, 0, 2) ZEND_ARG_INFO(0, low) @@ -3708,6 +3714,7 @@ zend_function_entry basic_functions[] = { PHP_FE(extract, arginfo_extract) PHP_FE(compact, arginfo_compact) PHP_FE(array_fill, arginfo_array_fill) + PHP_FE(array_fill_keys, arginfo_array_fill_keys) PHP_FE(range, arginfo_range) PHP_FE(array_multisort, arginfo_array_multisort) PHP_FE(array_push, arginfo_array_push) diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index 231f24f5aea..39c6913d7ad 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -54,6 +54,7 @@ PHP_FUNCTION(array_search); PHP_FUNCTION(extract); PHP_FUNCTION(compact); PHP_FUNCTION(array_fill); +PHP_FUNCTION(array_fill_keys); PHP_FUNCTION(range); PHP_FUNCTION(shuffle); PHP_FUNCTION(array_multisort); diff --git a/ext/standard/tests/array/array_fill_keys.phpt b/ext/standard/tests/array/array_fill_keys.phpt new file mode 100755 index 00000000000..f5356e1776a --- /dev/null +++ b/ext/standard/tests/array/array_fill_keys.phpt @@ -0,0 +1,74 @@ +--TEST-- +basic array_fill_keys test +--FILE-- + +--EXPECTF-- + +Warning: array_fill_keys(): First parameter must be an array in %s on line %d +bool(false) +array(0) { +} +array(2) { + ["foo"]=> + NULL + ["bar"]=> + NULL +} +array(4) { + [5]=> + int(123) + ["foo"]=> + int(123) + [10]=> + int(123) + ["1.23"]=> + int(123) +} +array(4) { + ["test"]=> + string(0) "" + [1]=> + string(0) "" + [10]=> + string(0) "" + [100]=> + string(0) "" +} +--UEXPECTF-- + +Warning: array_fill_keys(): First parameter must be an array in %s on line %d +bool(false) +array(0) { +} +array(2) { + [u"foo"]=> + NULL + [u"bar"]=> + NULL +} +array(4) { + [5]=> + int(123) + [u"foo"]=> + int(123) + [10]=> + int(123) + [u"1.23"]=> + int(123) +} +array(4) { + [u"test"]=> + unicode(0) "" + [1]=> + unicode(0) "" + [10]=> + unicode(0) "" + [100]=> + unicode(0) "" +}