Skip to content

Commit 38d9755

Browse files
committed
Fixed bug #68553 (array_column: null values in $index_key become incrementing keys in result)
1 parent 2139c2c commit 38d9755

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ PHP NEWS
1414
. Fixed bug #76488 (Memory leak when fetching a BLOB field). (Simonov Denis)
1515

1616
- Standard:
17+
. Fixed bug #68553 (array_column: null values in $index_key become incrementing
18+
keys in result). (Laruence)
1719
. Fixed bug #73817 (Incorrect entries in get_html_translation_table). (cmb)
1820

1921
- Zip:

ext/standard/array.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,16 +3526,38 @@ PHP_FUNCTION(array_column)
35263526
zkeyval = array_column_fetch_prop(data, zkey, &rvk);
35273527
}
35283528
if (zkeyval) {
3529-
if (Z_TYPE_P(zkeyval) == IS_STRING) {
3530-
zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(zkeyval), zcolval);
3531-
} else if (Z_TYPE_P(zkeyval) == IS_LONG) {
3532-
add_index_zval(return_value, Z_LVAL_P(zkeyval), zcolval);
3533-
} else if (Z_TYPE_P(zkeyval) == IS_OBJECT) {
3534-
zend_string *key = zval_get_string(zkeyval);
3535-
zend_symtable_update(Z_ARRVAL_P(return_value), key, zcolval);
3536-
zend_string_release(key);
3537-
} else {
3538-
add_next_index_zval(return_value, zcolval);
3529+
switch (Z_TYPE_P(zkeyval)) {
3530+
case IS_STRING:
3531+
zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(zkeyval), zcolval);
3532+
break;
3533+
case IS_LONG:
3534+
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(zkeyval), zcolval);
3535+
break;
3536+
case IS_OBJECT:
3537+
{
3538+
zend_string *key = zval_get_string(zkeyval);
3539+
zend_symtable_update(Z_ARRVAL_P(return_value), key, zcolval);
3540+
zend_string_release(key);
3541+
break;
3542+
}
3543+
case IS_NULL:
3544+
zend_hash_update(Z_ARRVAL_P(return_value), ZSTR_EMPTY_ALLOC(), zcolval);
3545+
break;
3546+
case IS_DOUBLE:
3547+
zend_hash_index_update(Z_ARRVAL_P(return_value), zend_dval_to_lval(Z_DVAL_P(zkeyval)), zcolval);
3548+
break;
3549+
case IS_TRUE:
3550+
zend_hash_index_update(Z_ARRVAL_P(return_value), 1, zcolval);
3551+
break;
3552+
case IS_FALSE:
3553+
zend_hash_index_update(Z_ARRVAL_P(return_value), 0, zcolval);
3554+
break;
3555+
case IS_RESOURCE:
3556+
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_RES_HANDLE_P(zkeyval), zcolval);
3557+
break;
3558+
default:
3559+
add_next_index_zval(return_value, zcolval);
3560+
break;
35393561
}
35403562
if (zkeyval == &rvk) {
35413563
zval_ptr_dtor(&rvk);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
Bug #68553 (array_column: null values in $index_key become incrementing keys in result)
3+
--FILE--
4+
<?php
5+
$i = 100;
6+
/* increase the resource id to make test stable */
7+
while ($i--) {
8+
$fd = fopen(__FILE__, "r");
9+
fclose($fd);
10+
}
11+
$a = [
12+
['a' => 10],
13+
['a' => 20],
14+
['a' => true],
15+
['a' => false],
16+
['a' => fopen(__FILE__, "r")],
17+
['a' => -5],
18+
['a' => 7.38],
19+
['a' => null, "test"],
20+
['a' => null],
21+
];
22+
23+
var_dump(array_column($a, null, 'a'));
24+
--EXPECTF--
25+
array(8) {
26+
[10]=>
27+
array(1) {
28+
["a"]=>
29+
int(10)
30+
}
31+
[20]=>
32+
array(1) {
33+
["a"]=>
34+
int(20)
35+
}
36+
[1]=>
37+
array(1) {
38+
["a"]=>
39+
bool(true)
40+
}
41+
[0]=>
42+
array(1) {
43+
["a"]=>
44+
bool(false)
45+
}
46+
[%d]=>
47+
array(1) {
48+
["a"]=>
49+
resource(%d) of type (stream)
50+
}
51+
[-5]=>
52+
array(1) {
53+
["a"]=>
54+
int(-5)
55+
}
56+
[7]=>
57+
array(1) {
58+
["a"]=>
59+
float(7.38)
60+
}
61+
[""]=>
62+
array(1) {
63+
["a"]=>
64+
NULL
65+
}
66+
}

0 commit comments

Comments
 (0)