Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality updates to array_column() #331

Closed
wants to merge 3 commits into from
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
91 changes: 85 additions & 6 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2532,17 +2532,22 @@ PHP_FUNCTION(array_column)
zval *zarray, **zcolumn, **zkey = NULL, **data, **zcolval, **zkeyval;
HashTable *arr_hash;
HashPosition pointer;
ulong column_idx = 0, key_idx = 0;
ulong column_idx = 0, key_idx = 0, column_is_null = 0;
char *column = NULL, *key = NULL, *keyval = NULL;
int column_len = 0, key_len = 0, keyval_idx = -1;

zval *column_arr = NULL, **column_data, **zcolval2;
HashTable *column_hash;
HashPosition column_pointer;
int column_is_array = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aZ|Z", &zarray, &zcolumn, &zkey) == FAILURE) {
return;
}

switch (Z_TYPE_PP(zcolumn)) {
case IS_NULL:
column_idx = 0;
column_is_null = 1;
break;
case IS_LONG:
column_idx = Z_LVAL_PP(zcolumn);
Expand All @@ -2556,8 +2561,12 @@ PHP_FUNCTION(array_column)
column = Z_STRVAL_PP(zcolumn);
column_len = Z_STRLEN_PP(zcolumn);
break;
case IS_ARRAY:
column_is_array = 1;
column_hash = Z_ARRVAL_PP(zcolumn);
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The column key should be either a string or an integer");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The column key should be a string, an integer, or an array");
RETURN_FALSE;
}

Expand Down Expand Up @@ -2594,13 +2603,79 @@ PHP_FUNCTION(array_column)
if (Z_TYPE_PP(data) == IS_ARRAY) {
zval *strkey = NULL;

if (column && zend_hash_find(Z_ARRVAL_PP(data), column, column_len + 1, (void**)&zcolval) == FAILURE) {
/* Find the current key/index of the array */
char *current_key;
uint current_key_len, hash_key_is_string = 0;
ulong current_index;
if (zend_hash_get_current_key_ex(arr_hash, &current_key, &current_key_len, &current_index, 0, &pointer) == HASH_KEY_IS_STRING) {
hash_key_is_string = 1;
}

if (column_is_null) {
zcolval = data;
} else if (column_is_array) {

MAKE_STD_ZVAL(column_arr);
array_init(column_arr);

for (zend_hash_internal_pointer_reset_ex(column_hash, &column_pointer);
zend_hash_get_current_data_ex(column_hash, (void**)&column_data, &column_pointer) == SUCCESS;
zend_hash_move_forward_ex(column_hash, &column_pointer)) {

zval *strobj = NULL;
column = NULL;

switch (Z_TYPE_PP(column_data)) {
case IS_LONG:
column_idx = Z_LVAL_PP(column_data);
break;
case IS_STRING:
column = Z_STRVAL_PP(column_data);
column_len = Z_STRLEN_PP(column_data);
break;
case IS_OBJECT:
{
MAKE_STD_ZVAL(strobj);
MAKE_COPY_ZVAL(column_data, strobj);
convert_to_string(strobj);
column = Z_STRVAL_P(strobj);
column_len = Z_STRLEN_P(strobj);
}
break;
default:
continue;
}

if (column && zend_hash_find(Z_ARRVAL_PP(data), column, column_len + 1, (void**)&zcolval2) == FAILURE) {
continue;
} else if (!column && zend_hash_index_find(Z_ARRVAL_PP(data), column_idx, (void**)&zcolval2) == FAILURE) {
continue;
}

Z_ADDREF_PP(zcolval2);

if (column) {
add_assoc_zval(column_arr, column, *zcolval2);
if (strobj) {
zval_ptr_dtor(&strobj);
}
} else {
add_index_zval(column_arr, column_idx, *zcolval2);
}

}

zcolval = &column_arr;

} else if (column && zend_hash_find(Z_ARRVAL_PP(data), column, column_len + 1, (void**)&zcolval) == FAILURE) {
continue;
} else if (!column && zend_hash_index_find(Z_ARRVAL_PP(data), column_idx, (void**)&zcolval) == FAILURE) {
continue;
}

Z_ADDREF_PP(zcolval);
if (column_arr == NULL) {
Z_ADDREF_PP(zcolval);
}

keyval = NULL;
keyval_idx = -1;
Expand Down Expand Up @@ -2639,8 +2714,12 @@ PHP_FUNCTION(array_column)
}
} else if (keyval_idx != -1) {
add_index_zval(return_value, keyval_idx, *zcolval);
} else {
} else if (zkey) {
add_next_index_zval(return_value, *zcolval);
} else if (hash_key_is_string) {
add_assoc_zval(return_value, current_key, *zcolval);
} else {
add_index_zval(return_value, current_index, *zcolval);
}
}

Expand Down
4 changes: 2 additions & 2 deletions ext/standard/tests/array/array_column_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ array(0) {

*** Testing columns not present in all rows ***
array(1) {
[0]=>
[1]=>
string(3) "qux"
}
array(1) {
Expand All @@ -277,7 +277,7 @@ array(3) {
array(2) {
[0]=>
string(3) "bar"
[1]=>
[2]=>
string(3) "fff"
}
array(2) {
Expand Down
12 changes: 2 additions & 10 deletions ext/standard/tests/array/array_column_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ var_dump(array_column(array(), true));
echo "\n-- Testing array_column() column key parameter should be a string or integer (testing float) --\n";
var_dump(array_column(array(), 2.3));

echo "\n-- Testing array_column() column key parameter should be a string or integer (testing array) --\n";
var_dump(array_column(array(), array()));

echo "\n-- Testing array_column() index key parameter should be a string or an integer (testing bool) --\n";
var_dump(array_column(array(), 'foo', true));

Expand Down Expand Up @@ -68,17 +65,12 @@ NULL

-- Testing array_column() column key parameter should be a string or an integer (testing bool) --

Warning: array_column(): The column key should be either a string or an integer in %s on line %d
Warning: array_column(): The column key should be a string, an integer, or an array in %s on line %d
bool(false)

-- Testing array_column() column key parameter should be a string or integer (testing float) --

Warning: array_column(): The column key should be either a string or an integer in %s on line %d
bool(false)

-- Testing array_column() column key parameter should be a string or integer (testing array) --

Warning: array_column(): The column key should be either a string or an integer in %s on line %d
Warning: array_column(): The column key should be a string, an integer, or an array in %s on line %d
bool(false)

-- Testing array_column() index key parameter should be a string or an integer (testing bool) --
Expand Down
22 changes: 22 additions & 0 deletions ext/standard/tests/array/array_column_object_cast.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class ColumnKeyClass {
function __toString() { return 'first_name'; }
}

class ColumnKeyClass2 {
function __toString() { return 'last_name'; }
}

class IndexKeyClass {
function __toString() { return 'id'; }
}
Expand All @@ -16,6 +20,7 @@ class ValueClass {


$column_key = new ColumnKeyClass();
$column_key2 = new ColumnKeyClass2();
$index_key = new IndexKeyClass();
$value = new ValueClass();

Expand All @@ -35,6 +40,8 @@ $records = array(
);
$firstNames = array_column($records, $column_key, $index_key);
print_r($firstNames);
$firstLastNames = array_column($records, array($column_key, $column_key2));
print_r($firstLastNames);
var_dump($column_key);
var_dump($index_key);
var_dump($value);
Expand All @@ -44,6 +51,21 @@ Array
[2135] => John
[3245] => Sally
)
Array
(
[0] => Array
(
[first_name] => John
[last_name] => XXX
)

[1] => Array
(
[first_name] => Sally
[last_name] => Smith
)

)
object(ColumnKeyClass)#%d (0) {
}
object(IndexKeyClass)#%d (0) {
Expand Down
Loading