Skip to content

Commit 0676de4

Browse files
committed
ext/standard: Add is_assoc_array() to detect associative arrays
1 parent c891263 commit 0676de4

File tree

5 files changed

+86
-8
lines changed

5 files changed

+86
-8
lines changed

ext/standard/array.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4385,6 +4385,39 @@ PHP_FUNCTION(array_keys)
43854385
}
43864386
/* }}} */
43874387

4388+
/* {{{ */
4389+
PHP_FUNCTION(is_assoc_array)
4390+
{
4391+
zval *arr;
4392+
zend_array *ht;
4393+
zend_ulong idx, expected = 0;
4394+
zend_string *key;
4395+
4396+
ZEND_PARSE_PARAMETERS_START(1,1)
4397+
Z_PARAM_ARRAY(arr)
4398+
ZEND_PARSE_PARAMETERS_END();
4399+
4400+
ht = Z_ARRVAL_P(arr);
4401+
4402+
if (zend_hash_num_elements(ht) == 0) {
4403+
RETURN_FALSE;
4404+
}
4405+
4406+
if(!HT_IS_PACKED(ht)){
4407+
RETURN_TRUE;
4408+
}
4409+
4410+
ZEND_HASH_FOREACH_KEY(ht, idx, key) {
4411+
if (key != NULL || idx != expected++) {
4412+
RETURN_TRUE; /* associative */
4413+
}
4414+
} ZEND_HASH_FOREACH_END();
4415+
4416+
RETURN_FALSE;
4417+
4418+
}
4419+
/* }}} */
4420+
43884421
/* {{{ Get the key of the first element of the array */
43894422
PHP_FUNCTION(array_key_first)
43904423
{

ext/standard/basic_functions.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,11 @@ function array_replace_recursive(array $array, array ...$replacements): array {}
16961696
*/
16971697
function array_keys(array $array, mixed $filter_value = UNKNOWN, bool $strict = false): array {}
16981698

1699+
/**
1700+
* @compile-time-eval
1701+
*/
1702+
function is_assoc_array(array $array): bool {}
1703+
16991704
/**
17001705
* @compile-time-eval
17011706
*/

ext/standard/basic_functions_arginfo.h

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/basic_functions_decl.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Test is_assoc_array() behavior
3+
--FILE--
4+
<?php
5+
6+
echo "1. "; var_dump(is_assoc_array(['a' => 'a', 0 => 'b']));
7+
echo "2. "; var_dump(is_assoc_array([1 => 'a', 0 => 'b']));
8+
echo "3. "; var_dump(is_assoc_array([1 => 'a', 2 => 'b']));
9+
echo "4. "; var_dump(is_assoc_array([0 => 'a', 1 => 'b']));
10+
echo "5. "; var_dump(is_assoc_array(['a', 'b']));
11+
12+
echo "6. "; var_dump(is_assoc_array([]));
13+
echo "7. "; var_dump(is_assoc_array([1, 2, 3]));
14+
echo "8. "; var_dump(is_assoc_array(['foo', 2, 3]));
15+
echo "9. "; var_dump(is_assoc_array([0 => 'foo', 'bar']));
16+
17+
echo "10. "; var_dump(is_assoc_array([1 => 'foo', 'bar']));
18+
echo "11. "; var_dump(is_assoc_array([0 => 'foo', 'bar' => 'baz']));
19+
echo "12. "; var_dump(is_assoc_array([0 => 'foo', 2 => 'bar']));
20+
echo "13. "; var_dump(is_assoc_array(['foo' => 'bar', 'baz' => 'qux']));
21+
22+
?>
23+
--EXPECT--
24+
1. bool(true)
25+
2. bool(true)
26+
3. bool(true)
27+
4. bool(false)
28+
5. bool(false)
29+
6. bool(false)
30+
7. bool(false)
31+
8. bool(false)
32+
9. bool(false)
33+
10. bool(true)
34+
11. bool(true)
35+
12. bool(true)
36+
13. bool(true)

0 commit comments

Comments
 (0)