diff options
author | Marcus Boerger <helly@php.net> | 2006-07-15 10:21:10 +0000 |
---|---|---|
committer | Marcus Boerger <helly@php.net> | 2006-07-15 10:21:10 +0000 |
commit | 87c64280b0b6a03e61dae5a62ece5169f706938d (patch) | |
tree | e6b5638d5d8db295930d95afc1f5982e1e236342 /ext/standard/array.c | |
parent | 54dba635a31790f9b7802c258a5b85b680348a08 (diff) | |
download | php-git-87c64280b0b6a03e61dae5a62ece5169f706938d.tar.gz |
- MFH Added array_fill_keys(). (Marcus, Mathew W)
Diffstat (limited to 'ext/standard/array.c')
-rw-r--r-- | ext/standard/array.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c index a0324302d9..76d3e30022 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1720,6 +1720,56 @@ err: } /* }}} */ +/* {{{ 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) { + zend_symtable_update(Z_ARRVAL_P(return_value), Z_STRVAL_PP(entry), Z_STRLEN_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); + } +} +/* }}} */ static void array_data_shuffle(zval *array TSRMLS_DC) { |