Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4385,6 +4385,39 @@ PHP_FUNCTION(array_keys)
}
/* }}} */

/* {{{ */
PHP_FUNCTION(is_assoc_array)
{
zval *arr;
zend_array *ht;
zend_ulong idx, expected = 0;
zend_string *key;

ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_ARRAY(arr)
ZEND_PARSE_PARAMETERS_END();

ht = Z_ARRVAL_P(arr);

if (zend_hash_num_elements(ht) == 0) {
RETURN_FALSE;
}

if(!HT_IS_PACKED(ht)){
RETURN_TRUE;
}

ZEND_HASH_FOREACH_KEY(ht, idx, key) {
if (key != NULL || idx != expected++) {
RETURN_TRUE; /* associative */
}
} ZEND_HASH_FOREACH_END();

RETURN_FALSE;

}
/* }}} */

/* {{{ Get the key of the first element of the array */
PHP_FUNCTION(array_key_first)
{
Expand Down
5 changes: 5 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,11 @@ function array_replace_recursive(array $array, array ...$replacements): array {}
*/
function array_keys(array $array, mixed $filter_value = UNKNOWN, bool $strict = false): array {}

/**
* @compile-time-eval
*/
function is_assoc_array(array $array): bool {}

/**
* @compile-time-eval
*/
Expand Down
12 changes: 8 additions & 4 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ext/standard/basic_functions_decl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions ext/standard/tests/array/is_assoc_array.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Test is_assoc_array() behavior
--FILE--
<?php

echo "1. "; var_dump(is_assoc_array(['a' => 'a', 0 => 'b']));
echo "2. "; var_dump(is_assoc_array([1 => 'a', 0 => 'b']));
echo "3. "; var_dump(is_assoc_array([1 => 'a', 2 => 'b']));
echo "4. "; var_dump(is_assoc_array([0 => 'a', 1 => 'b']));
echo "5. "; var_dump(is_assoc_array(['a', 'b']));

echo "6. "; var_dump(is_assoc_array([]));
echo "7. "; var_dump(is_assoc_array([1, 2, 3]));
echo "8. "; var_dump(is_assoc_array(['foo', 2, 3]));
echo "9. "; var_dump(is_assoc_array([0 => 'foo', 'bar']));

echo "10. "; var_dump(is_assoc_array([1 => 'foo', 'bar']));
echo "11. "; var_dump(is_assoc_array([0 => 'foo', 'bar' => 'baz']));
echo "12. "; var_dump(is_assoc_array([0 => 'foo', 2 => 'bar']));
echo "13. "; var_dump(is_assoc_array(['foo' => 'bar', 'baz' => 'qux']));

?>
--EXPECT--
1. bool(true)
2. bool(true)
3. bool(true)
4. bool(false)
5. bool(false)
6. bool(false)
7. bool(false)
8. bool(false)
9. bool(false)
10. bool(true)
11. bool(true)
12. bool(true)
13. bool(true)
Loading