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

Fix saving of -1 index from keys column in array_column function #317

Closed
wants to merge 1 commit 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
40 changes: 21 additions & 19 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2534,7 +2534,8 @@ PHP_FUNCTION(array_column)
HashPosition pointer;
ulong column_idx = 0, key_idx = 0;
char *column = NULL, *key = NULL, *keyval = NULL;
int column_len = 0, key_len = 0, keyval_idx = -1;
zend_bool use_keyval = 0;
int column_len = 0, key_len = 0, keyval_len = 0, keyval_idx;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aZ|Z", &zarray, &zcolumn, &zkey) == FAILURE) {
return;
Expand Down Expand Up @@ -2602,48 +2603,49 @@ PHP_FUNCTION(array_column)

Z_ADDREF_PP(zcolval);

use_keyval = 0;
keyval = NULL;
keyval_idx = -1;

if (zkey) {
if (key && zend_hash_find(Z_ARRVAL_PP(data), key, key_len + 1, (void**)&zkeyval) == FAILURE) {
keyval_idx = -1;
} else if (!key && zend_hash_index_find(Z_ARRVAL_PP(data), key_idx, (void**)&zkeyval) == FAILURE) {
keyval_idx = -1;
} else {
switch (Z_TYPE_PP(zkeyval)) {
if (zkey && ((key && zend_hash_find(Z_ARRVAL_PP(data), key, key_len + 1, (void**)&zkeyval) != FAILURE) ||
(!key && zend_hash_index_find(Z_ARRVAL_PP(data), key_idx, (void**)&zkeyval) != FAILURE))) {
use_keyval = 1;
switch (Z_TYPE_PP(zkeyval)) {
case IS_LONG:
keyval_idx = Z_LVAL_PP(zkeyval);
break;
case IS_STRING:
keyval = Z_STRVAL_PP(zkeyval);
keyval_len = Z_STRLEN_PP(zkeyval);
break;
case IS_OBJECT:
{
MAKE_STD_ZVAL(strkey);
MAKE_COPY_ZVAL(zkeyval, strkey);
convert_to_string(strkey);
keyval = Z_STRVAL_P(strkey);
keyval_len = Z_STRLEN_P(strkey);
}
break;
default:
keyval_idx = -1;
}
use_keyval = 0;
}
}

if (keyval) {
add_assoc_zval(return_value, keyval, *zcolval);
if (strkey) {
zval_ptr_dtor(&strkey);

if (use_keyval) {
if (keyval) {
add_assoc_zval_ex(return_value, keyval, keyval_len+1, *zcolval);
if (strkey) {
zval_ptr_dtor(&strkey);
}
}
else {
add_index_zval(return_value, keyval_idx, *zcolval);
}
} else if (keyval_idx != -1) {
add_index_zval(return_value, keyval_idx, *zcolval);
} else {
add_next_index_zval(return_value, *zcolval);
}
}

}
}
/* }}} */
Expand Down
33 changes: 33 additions & 0 deletions ext/standard/tests/array/array_column_minus_one_index.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Test array_column() function: basic functionality (saving of -1 index)
--FILE--
<?php
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe'
),
array(
'id' => -1,
'first_name' => 'Sally',
'last_name' => 'Smith'
),
array(
'id' => -2,
'first_name' => 'Jane',
'last_name' => 'Jones'
),
);

$firstNames = array_column($records, 'first_name', 'id');
print_r($firstNames);
?>
--EXPECTF--
Array
(
[2135] => John
[-1] => Sally
[-2] => Jane
)