diff options
author | Dusk <dusk@woofle.net> | 2019-11-03 18:51:49 -0800 |
---|---|---|
committer | Tyson Andre <tysonandre775@hotmail.com> | 2021-01-20 18:53:48 -0500 |
commit | 13c430b1db0ca1595122ce552ce76f84b32795f4 (patch) | |
tree | fbcbc5f25971911f3d73eb3c9f85e20d697fed59 /ext/json | |
parent | 04db2c873588633daf6639a7de4a3ffd5936c47c (diff) | |
download | php-git-13c430b1db0ca1595122ce552ce76f84b32795f4.tar.gz |
Add array_is_list(array $array) function
This function tests if an array contains only sequential integer keys. While
list isn't an official type, this usage is consistent with the community usage
of "list" as an annotation type, cf.
https://psalm.dev/docs/annotating_code/type_syntax/array_types/#lists
Rebased and modified version of #4886
- Use .stub.php files
- Add opcache constant evaluation when argument is a constant
- Change from is_list(mixed $value) to array_is_list(array $array)
RFC: https://wiki.php.net/rfc/is_list
Co-Authored-By: Tyson Andre <tysonandre775@hotmail.com>
Co-Authored-By: Dusk <dusk@woofle.net>
Closes GH-6070
Diffstat (limited to 'ext/json')
-rw-r--r-- | ext/json/json_encoder.c | 25 |
1 files changed, 3 insertions, 22 deletions
diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c index 92e4a10933..f608936349 100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@ -36,29 +36,10 @@ static int php_json_escape_string( static int php_json_determine_array_type(zval *val) /* {{{ */ { - int i; - HashTable *myht = Z_ARRVAL_P(val); - - i = myht ? zend_hash_num_elements(myht) : 0; - if (i > 0) { - zend_string *key; - zend_ulong index, idx; + zend_array *myht = Z_ARRVAL_P(val); - if (HT_IS_PACKED(myht) && HT_IS_WITHOUT_HOLES(myht)) { - return PHP_JSON_OUTPUT_ARRAY; - } - - idx = 0; - ZEND_HASH_FOREACH_KEY(myht, index, key) { - if (key) { - return PHP_JSON_OUTPUT_OBJECT; - } else { - if (index != idx) { - return PHP_JSON_OUTPUT_OBJECT; - } - } - idx++; - } ZEND_HASH_FOREACH_END(); + if (myht) { + return zend_array_is_list(myht) ? PHP_JSON_OUTPUT_ARRAY : PHP_JSON_OUTPUT_OBJECT; } return PHP_JSON_OUTPUT_ARRAY; |