]> granicus.if.org Git - php/commitdiff
Fixed bug #68553 (array_column: null values in $index_key become incrementing keys...
authorXinchen Hui <laruence@gmail.com>
Tue, 24 Jul 2018 03:34:57 +0000 (11:34 +0800)
committerXinchen Hui <laruence@gmail.com>
Tue, 24 Jul 2018 03:34:57 +0000 (11:34 +0800)
NEWS
ext/standard/array.c
ext/standard/tests/array/bug68553.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index c2aad666aeac5a9c102519f335b7ff3419d3b464..2c12a3422aac926d29ebde6c39bd6767a4c92161 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,8 @@ PHP                                                                        NEWS
   . Fixed bug #76488 (Memory leak when fetching a BLOB field). (Simonov Denis)
 
 - Standard:
+  . Fixed bug #68553 (array_column: null values in $index_key become incrementing
+    keys in result). (Laruence)
   . Fixed bug #73817 (Incorrect entries in get_html_translation_table). (cmb)
 
 - Zip:
index 646ffad19897ecdcec0ae1561cf8e039d69f4140..a1e3faa3ddc44b9def7bbee243409312c3c03c93 100644 (file)
@@ -3526,16 +3526,38 @@ PHP_FUNCTION(array_column)
                                zkeyval = array_column_fetch_prop(data, zkey, &rvk);
                        }
                        if (zkeyval) {
-                               if (Z_TYPE_P(zkeyval) == IS_STRING) {
-                                       zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(zkeyval), zcolval);
-                               } else if (Z_TYPE_P(zkeyval) == IS_LONG) {
-                                       add_index_zval(return_value, Z_LVAL_P(zkeyval), zcolval);
-                               } else if (Z_TYPE_P(zkeyval) == IS_OBJECT) {
-                                       zend_string *key = zval_get_string(zkeyval);
-                                       zend_symtable_update(Z_ARRVAL_P(return_value), key, zcolval);
-                                       zend_string_release(key);
-                               } else {
-                                       add_next_index_zval(return_value, zcolval);
+                               switch (Z_TYPE_P(zkeyval)) {
+                                       case IS_STRING:
+                                               zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(zkeyval), zcolval);
+                                               break;
+                                       case IS_LONG:
+                                               zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(zkeyval), zcolval);
+                                               break;
+                                       case IS_OBJECT:
+                                       {
+                                               zend_string *key = zval_get_string(zkeyval);
+                                               zend_symtable_update(Z_ARRVAL_P(return_value), key, zcolval);
+                                               zend_string_release(key);
+                                               break;
+                                       }
+                                       case IS_NULL:
+                                               zend_hash_update(Z_ARRVAL_P(return_value), ZSTR_EMPTY_ALLOC(), zcolval);
+                                               break;
+                                       case IS_DOUBLE:
+                                               zend_hash_index_update(Z_ARRVAL_P(return_value), zend_dval_to_lval(Z_DVAL_P(zkeyval)), zcolval);
+                                               break;
+                                       case IS_TRUE:
+                                               zend_hash_index_update(Z_ARRVAL_P(return_value), 1, zcolval);
+                                               break;
+                                       case IS_FALSE:
+                                               zend_hash_index_update(Z_ARRVAL_P(return_value), 0, zcolval);
+                                               break;
+                                       case IS_RESOURCE:
+                                               zend_hash_index_update(Z_ARRVAL_P(return_value), Z_RES_HANDLE_P(zkeyval), zcolval);
+                                               break;
+                                       default:
+                                               add_next_index_zval(return_value, zcolval);
+                                               break;
                                }
                                if (zkeyval == &rvk) {
                                        zval_ptr_dtor(&rvk);
diff --git a/ext/standard/tests/array/bug68553.phpt b/ext/standard/tests/array/bug68553.phpt
new file mode 100644 (file)
index 0000000..91c5b08
--- /dev/null
@@ -0,0 +1,66 @@
+--TEST--
+Bug #68553 (array_column: null values in $index_key become incrementing keys in result)
+--FILE--
+<?php
+$i = 100;
+/* increase the resource id to make test stable */
+while ($i--) {
+       $fd = fopen(__FILE__, "r");
+       fclose($fd);
+}
+$a = [ 
+       ['a' => 10],
+       ['a' => 20],
+       ['a' => true],
+       ['a' => false],
+       ['a' => fopen(__FILE__, "r")],
+       ['a' => -5],
+       ['a' => 7.38],
+       ['a' => null, "test"],
+       ['a' => null],
+];
+
+var_dump(array_column($a, null, 'a'));
+--EXPECTF--
+array(8) {
+  [10]=>
+  array(1) {
+    ["a"]=>
+    int(10)
+  }
+  [20]=>
+  array(1) {
+    ["a"]=>
+    int(20)
+  }
+  [1]=>
+  array(1) {
+    ["a"]=>
+    bool(true)
+  }
+  [0]=>
+  array(1) {
+    ["a"]=>
+    bool(false)
+  }
+  [%d]=>
+  array(1) {
+    ["a"]=>
+    resource(%d) of type (stream)
+  }
+  [-5]=>
+  array(1) {
+    ["a"]=>
+    int(-5)
+  }
+  [7]=>
+  array(1) {
+    ["a"]=>
+    float(7.38)
+  }
+  [""]=>
+  array(1) {
+    ["a"]=>
+    NULL
+  }
+}