diff --git a/NEWS b/NEWS index 491e526d6b8f..0f8b4fbb20c3 100644 --- a/NEWS +++ b/NEWS @@ -294,6 +294,7 @@ PHP NEWS (Nicolas Grekas) . Fixed convert.quoted-printable-decode decoding lowercase hex digits to the wrong byte. (Ilia Alshanetsky) + . Fixed boolean key handling in array_combine(). (arshidkv12) - Tidy: . Fixed a use-after-free when a tidyNode is used after its document is diff --git a/ext/standard/array.c b/ext/standard/array.c index ebd114844ab9..83c11b648e2a 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -7187,6 +7187,11 @@ PHP_FUNCTION(array_combine) if (Z_TYPE_P(entry_keys) == IS_LONG) { entry_values = zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(entry_keys), entry_values); + } else if (Z_TYPE_P(entry_keys) == IS_FALSE || Z_TYPE_P(entry_keys) == IS_TRUE) { + entry_values = zend_hash_index_update(Z_ARRVAL_P(return_value), + Z_TYPE_P(entry_keys) == IS_TRUE ? 1 : 0, + entry_values + ); } else { zend_string *tmp_key; zend_string *key = zval_get_tmp_string(entry_keys, &tmp_key); diff --git a/ext/standard/tests/array/array_combine_boolean_keys.phpt b/ext/standard/tests/array/array_combine_boolean_keys.phpt new file mode 100644 index 000000000000..efc80a44f02e --- /dev/null +++ b/ext/standard/tests/array/array_combine_boolean_keys.phpt @@ -0,0 +1,39 @@ +--TEST-- +array_combine() with boolean keys compared to direct array keys +--FILE-- + 'true', false => 'false']); + +echo "\narray_combine:\n"; +var_dump(array_combine([true, false], ['true', 'false'])); + +echo "\nExplicit integer keys:\n"; +var_dump(array_combine([1, 0], ['true', 'false'])); + +?> +--EXPECT-- +Direct array keys: +array(2) { + [1]=> + string(4) "true" + [0]=> + string(5) "false" +} + +array_combine: +array(2) { + [1]=> + string(4) "true" + [0]=> + string(5) "false" +} + +Explicit integer keys: +array(2) { + [1]=> + string(4) "true" + [0]=> + string(5) "false" +}