Skip to content

Commit c24f011

Browse files
committed
separate in a new function
1 parent 0ca49e9 commit c24f011

File tree

2 files changed

+87
-60
lines changed

2 files changed

+87
-60
lines changed

ext/standard/array.c

+79-59
Original file line numberDiff line numberDiff line change
@@ -4300,11 +4300,22 @@ PHP_FUNCTION(array_column)
43004300
void* cache_slot_index[3] = { NULL, NULL, NULL };
43014301

43024302
array_init_size(return_value, zend_hash_num_elements(input));
4303-
if (!group_is_null && group_bool && !index_is_null) {
4304-
4305-
zval *group_entry;
4306-
zval fpal;
4307-
4303+
/* Index param is not passed */
4304+
if (index_is_null) {
4305+
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
4306+
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
4307+
ZEND_HASH_FOREACH_VAL(input, data) {
4308+
ZVAL_DEREF(data);
4309+
if (column_is_null) {
4310+
Z_TRY_ADDREF_P(data);
4311+
colval = data;
4312+
} else if ((colval = array_column_fetch_prop(data, column_str, column_long, cache_slot_column, &rv)) == NULL) {
4313+
continue;
4314+
}
4315+
ZEND_HASH_FILL_ADD(colval);
4316+
} ZEND_HASH_FOREACH_END();
4317+
} ZEND_HASH_FILL_END();
4318+
} else {
43084319
ZEND_HASH_FOREACH_VAL(input, data) {
43094320
ZVAL_DEREF(data);
43104321

@@ -4317,72 +4328,81 @@ PHP_FUNCTION(array_column)
43174328

43184329
zval rv;
43194330
zval *keyval = array_column_fetch_prop(data, index_str, index_long, cache_slot_index, &rv);
4320-
43214331
if (keyval) {
4332+
array_set_zval_key(Z_ARRVAL_P(return_value), keyval, colval);
4333+
zval_ptr_dtor(colval);
4334+
zval_ptr_dtor(keyval);
4335+
} else {
4336+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), colval);
4337+
}
4338+
} ZEND_HASH_FOREACH_END();
4339+
}
4340+
}
4341+
/* }}} */
43224342

4323-
if (Z_TYPE_P(keyval) == IS_LONG) {
4324-
group_entry = zend_hash_index_find(Z_ARRVAL_P(return_value), Z_LVAL_P(keyval));
4325-
} else {
4326-
group_entry = zend_symtable_find(Z_ARRVAL_P(return_value), Z_STR_P(keyval));
4327-
}
4343+
/* {{{ Return all the values from a single column in the input array, identified by the
4344+
value_key and optionally indexed by the index_key in an indexed array */
4345+
PHP_FUNCTION(array_column_group)
4346+
{
4347+
HashTable *input;
4348+
zval *colval, *data, rv;
4349+
zend_string *column_str = NULL;
4350+
zend_long column_long;
4351+
bool column_is_null = 0;
4352+
zend_string *index_str = NULL;
4353+
zend_long index_long;
4354+
bool index_is_null = 1;
43284355

4329-
if (group_entry != NULL) {
4330-
ZVAL_COPY_VALUE(&fpal, group_entry);
4331-
} else {
4332-
array_init(&fpal);
4333-
array_set_zval_key(Z_ARRVAL_P(return_value), keyval, &fpal);
4334-
zval_ptr_dtor(&fpal);
4335-
}
4356+
ZEND_PARSE_PARAMETERS_START(2, 3)
4357+
Z_PARAM_ARRAY_HT(input)
4358+
Z_PARAM_STR_OR_LONG_OR_NULL(column_str, column_long, column_is_null)
4359+
Z_PARAM_OPTIONAL
4360+
Z_PARAM_STR_OR_LONG_OR_NULL(index_str, index_long, index_is_null)
4361+
ZEND_PARSE_PARAMETERS_END();
4362+
4363+
void* cache_slot_column[3] = { NULL, NULL, NULL };
4364+
void* cache_slot_index[3] = { NULL, NULL, NULL };
43364365

4337-
zend_hash_next_index_insert_new(Z_ARRVAL_P(&fpal), colval);
4366+
zval *group_entry;
4367+
zval fpal;
43384368

4339-
zval_ptr_dtor(keyval);
4369+
array_init_size(return_value, zend_hash_num_elements(input));
4370+
ZEND_HASH_FOREACH_VAL(input, data) {
4371+
ZVAL_DEREF(data);
4372+
4373+
if (column_is_null) {
4374+
Z_TRY_ADDREF_P(data);
4375+
colval = data;
4376+
} else if ((colval = array_column_fetch_prop(data, column_str, column_long, cache_slot_column, &rv)) == NULL) {
4377+
continue;
4378+
}
4379+
4380+
zval rv;
4381+
zval *keyval = array_column_fetch_prop(data, index_str, index_long, cache_slot_index, &rv);
4382+
4383+
if (keyval) {
4384+
4385+
if (Z_TYPE_P(keyval) == IS_LONG) {
4386+
group_entry = zend_hash_index_find(Z_ARRVAL_P(return_value), Z_LVAL_P(keyval));
43404387
} else {
4341-
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), colval);
4388+
group_entry = zend_symtable_find(Z_ARRVAL_P(return_value), Z_STR_P(keyval));
43424389
}
43434390

4391+
if (group_entry != NULL) {
4392+
ZVAL_COPY_VALUE(&fpal, group_entry);
4393+
} else {
4394+
array_init(&fpal);
4395+
array_set_zval_key(Z_ARRVAL_P(return_value), keyval, &fpal);
4396+
zval_ptr_dtor(&fpal);
4397+
}
43444398

4345-
} ZEND_HASH_FOREACH_END();
4399+
zend_hash_next_index_insert_new(Z_ARRVAL_P(&fpal), colval);
43464400

4347-
} else {
4348-
/* Index param is not passed */
4349-
if (index_is_null) {
4350-
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
4351-
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
4352-
ZEND_HASH_FOREACH_VAL(input, data) {
4353-
ZVAL_DEREF(data);
4354-
if (column_is_null) {
4355-
Z_TRY_ADDREF_P(data);
4356-
colval = data;
4357-
} else if ((colval = array_column_fetch_prop(data, column_str, column_long, cache_slot_column, &rv)) == NULL) {
4358-
continue;
4359-
}
4360-
ZEND_HASH_FILL_ADD(colval);
4361-
} ZEND_HASH_FOREACH_END();
4362-
} ZEND_HASH_FILL_END();
4401+
zval_ptr_dtor(keyval);
43634402
} else {
4364-
ZEND_HASH_FOREACH_VAL(input, data) {
4365-
ZVAL_DEREF(data);
4366-
4367-
if (column_is_null) {
4368-
Z_TRY_ADDREF_P(data);
4369-
colval = data;
4370-
} else if ((colval = array_column_fetch_prop(data, column_str, column_long, cache_slot_column, &rv)) == NULL) {
4371-
continue;
4372-
}
4373-
4374-
zval rv;
4375-
zval *keyval = array_column_fetch_prop(data, index_str, index_long, cache_slot_index, &rv);
4376-
if (keyval) {
4377-
array_set_zval_key(Z_ARRVAL_P(return_value), keyval, colval);
4378-
zval_ptr_dtor(colval);
4379-
zval_ptr_dtor(keyval);
4380-
} else {
4381-
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), colval);
4382-
}
4383-
} ZEND_HASH_FOREACH_END();
4403+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), colval);
43844404
}
4385-
}
4405+
} ZEND_HASH_FOREACH_END();
43864406
}
43874407
/* }}} */
43884408

ext/standard/basic_functions_arginfo.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_column, 0, 2, IS_ARRAY, 0)
245245
ZEND_ARG_TYPE_INFO(0, array, IS_ARRAY, 0)
246246
ZEND_ARG_TYPE_MASK(0, column_key, MAY_BE_LONG|MAY_BE_STRING|MAY_BE_NULL, NULL)
247247
ZEND_ARG_TYPE_MASK(0, index_key, MAY_BE_LONG|MAY_BE_STRING|MAY_BE_NULL, "null")
248-
ZEND_ARG_TYPE_MASK(0, group_key, MAY_BE_BOOL|MAY_BE_NULL, "null")
248+
ZEND_END_ARG_INFO()
249+
250+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_column_group, 0, 2, IS_ARRAY, 0)
251+
ZEND_ARG_TYPE_INFO(0, array, IS_ARRAY, 0)
252+
ZEND_ARG_TYPE_MASK(0, column_key, MAY_BE_LONG|MAY_BE_STRING|MAY_BE_NULL, NULL)
253+
ZEND_ARG_TYPE_MASK(0, index_key, MAY_BE_LONG|MAY_BE_STRING|MAY_BE_NULL, "null")
249254
ZEND_END_ARG_INFO()
250255

251256
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_reverse, 0, 1, IS_ARRAY, 0)
@@ -2288,6 +2293,7 @@ ZEND_FUNCTION(array_key_last);
22882293
ZEND_FUNCTION(array_values);
22892294
ZEND_FUNCTION(array_count_values);
22902295
ZEND_FUNCTION(array_column);
2296+
ZEND_FUNCTION(array_column_group);
22912297
ZEND_FUNCTION(array_reverse);
22922298
ZEND_FUNCTION(array_pad);
22932299
ZEND_FUNCTION(array_flip);
@@ -2916,6 +2922,7 @@ static const zend_function_entry ext_functions[] = {
29162922
ZEND_FE(array_values, arginfo_array_values)
29172923
ZEND_FE(array_count_values, arginfo_array_count_values)
29182924
ZEND_FE(array_column, arginfo_array_column)
2925+
ZEND_FE(array_column_group, arginfo_array_column_group)
29192926
ZEND_FE(array_reverse, arginfo_array_reverse)
29202927
ZEND_FE(array_pad, arginfo_array_pad)
29212928
ZEND_FE(array_flip, arginfo_array_flip)

0 commit comments

Comments
 (0)