Skip to content

Commit 75a9a5f

Browse files
authored
Add zend_array_to_list() (#8976)
* Add zend_array_to_list() * Use `zend_array_to_list()` in `PHP_FUNCTION(array_values)`
1 parent 23654a1 commit 75a9a5f

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

Zend/zend_hash.c

+20
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,26 @@ ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source)
23962396
return target;
23972397
}
23982398

2399+
ZEND_API HashTable* zend_array_to_list(HashTable *source)
2400+
{
2401+
HashTable *result = _zend_new_array(zend_hash_num_elements(source));
2402+
zend_hash_real_init_packed(result);
2403+
2404+
ZEND_HASH_FILL_PACKED(result) {
2405+
zval *entry;
2406+
2407+
ZEND_HASH_FOREACH_VAL(source, entry) {
2408+
if (UNEXPECTED(Z_ISREF_P(entry) && Z_REFCOUNT_P(entry) == 1)) {
2409+
entry = Z_REFVAL_P(entry);
2410+
}
2411+
Z_TRY_ADDREF_P(entry);
2412+
ZEND_HASH_FILL_ADD(entry);
2413+
} ZEND_HASH_FOREACH_END();
2414+
} ZEND_HASH_FILL_END();
2415+
2416+
return result;
2417+
}
2418+
23992419

24002420
ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, bool overwrite)
24012421
{

Zend/zend_hash.h

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(zval *val1, zval *val2);
320320
ZEND_API uint32_t zend_array_count(HashTable *ht);
321321
ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source);
322322
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht);
323+
ZEND_API HashTable* zend_array_to_list(HashTable *source);
323324
ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht);
324325
ZEND_API HashTable* ZEND_FASTCALL zend_symtable_to_proptable(HashTable *ht);
325326
ZEND_API HashTable* ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, bool always_duplicate);

ext/standard/array.c

+2-16
Original file line numberDiff line numberDiff line change
@@ -4147,8 +4147,7 @@ PHP_FUNCTION(array_key_last)
41474147
/* {{{ Return just the values from the input array */
41484148
PHP_FUNCTION(array_values)
41494149
{
4150-
zval *input, /* Input array */
4151-
*entry; /* An entry in the input array */
4150+
zval *input; /* Input array */
41524151
zend_array *arrval;
41534152
zend_long arrlen;
41544153

@@ -4170,20 +4169,7 @@ PHP_FUNCTION(array_values)
41704169
RETURN_COPY(input);
41714170
}
41724171

4173-
/* Initialize return array */
4174-
array_init_size(return_value, arrlen);
4175-
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
4176-
4177-
/* Go through input array and add values to the return array */
4178-
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
4179-
ZEND_HASH_FOREACH_VAL(arrval, entry) {
4180-
if (UNEXPECTED(Z_ISREF_P(entry) && Z_REFCOUNT_P(entry) == 1)) {
4181-
entry = Z_REFVAL_P(entry);
4182-
}
4183-
Z_TRY_ADDREF_P(entry);
4184-
ZEND_HASH_FILL_ADD(entry);
4185-
} ZEND_HASH_FOREACH_END();
4186-
} ZEND_HASH_FILL_END();
4172+
RETURN_ARR(zend_array_to_list(arrval));
41874173
}
41884174
/* }}} */
41894175

0 commit comments

Comments
 (0)