From 33c2d0e0709d474053719d35cdf96b864a38569b Mon Sep 17 00:00:00 2001 From: Andrew Faulds Date: Fri, 13 Jul 2012 23:12:48 +0100 Subject: [PATCH 1/2] Added array_last_key() function, returns key of last array item - Added array_last_key to standard extension - Added array_last_key test to standard extension tests --- ext/standard/array.c | 32 ++++++++++++++++++++ ext/standard/basic_functions.c | 5 +++ ext/standard/php_array.h | 1 + ext/standard/tests/array/array_last_key.phpt | 24 +++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 ext/standard/tests/array/array_last_key.phpt diff --git a/ext/standard/array.c b/ext/standard/array.c index 94c5e7e6ee7de..0b9c1855f5dc6 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -948,6 +948,38 @@ PHP_FUNCTION(key) } /* }}} */ +/* {{{ proto array array_last_key(array input) + Returns last key in the array */ +PHP_FUNCTION(array_last_key) +{ + HashTable *array; + char *string_key; + uint string_key_len; + ulong num_key; + Bucket *bucket; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) { + return; + } + + bucket = array->pListTail; + if (bucket) { + string_key_len = bucket->nKeyLength; + /* if the key length > 0, it's a hashed string, else numeric key */ + if (string_key_len > 0) { + string_key = bucket->arKey; + /* length - 1 because null-terminated... */ + RETVAL_STRINGL(string_key, string_key_len - 1, 1); + } else { + num_key = bucket->h; + RETVAL_LONG(num_key); + } + } else { + return; /* empty array */ + } +} +/* }}} */ + /* {{{ proto mixed min(mixed arg1 [, mixed arg2 [, mixed ...]]) Return the lowest value in an array or a series of arguments */ PHP_FUNCTION(min) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 771108b8d83db..54d2571d28682 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -296,6 +296,10 @@ ZEND_BEGIN_ARG_INFO(arginfo_key, ZEND_SEND_PREFER_REF) ZEND_ARG_INFO(ZEND_SEND_PREFER_REF, arg) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_array_last_key, 0) + ZEND_ARG_INFO(0, arg) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_min, 0, 0, 1) ZEND_ARG_INFO(0, arg1) ZEND_ARG_INFO(0, arg2) @@ -3293,6 +3297,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(reset, arginfo_reset) PHP_FE(current, arginfo_current) PHP_FE(key, arginfo_key) + PHP_FE(array_last_key, arginfo_array_last_key) PHP_FE(min, arginfo_min) PHP_FE(max, arginfo_max) PHP_FE(in_array, arginfo_in_array) diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index e63e01c61ee65..771cd1d74007a 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -47,6 +47,7 @@ PHP_FUNCTION(next); PHP_FUNCTION(reset); PHP_FUNCTION(current); PHP_FUNCTION(key); +PHP_FUNCTION(array_last_key); PHP_FUNCTION(min); PHP_FUNCTION(max); PHP_FUNCTION(in_array); diff --git a/ext/standard/tests/array/array_last_key.phpt b/ext/standard/tests/array/array_last_key.phpt new file mode 100644 index 0000000000000..4e3119e2e7bc8 --- /dev/null +++ b/ext/standard/tests/array/array_last_key.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test array_last_key() function +--FILE-- + 7]; +var_dump( array_last_key($a) ); + +echo "Done\n"; +?> +--EXPECTF-- +NULL +int(0) +int(1) +string(6) "foobar" +Done From 62baf952de1d496e99c13a61795c5e239a3df730 Mon Sep 17 00:00:00 2001 From: Andrew Faulds Date: Sat, 14 Jul 2012 11:57:52 +0100 Subject: [PATCH 2/2] Added array_first_key() function, returns key of first array item - Added array_first_key to standard extension - Added array_first_key test to standard extension tests --- ext/standard/array.c | 32 +++++++++++++++++++ ext/standard/basic_functions.c | 5 +++ ext/standard/php_array.h | 1 + ext/standard/tests/array/array_first_key.phpt | 24 ++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 ext/standard/tests/array/array_first_key.phpt diff --git a/ext/standard/array.c b/ext/standard/array.c index 0b9c1855f5dc6..c76febb0b5131 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -980,6 +980,38 @@ PHP_FUNCTION(array_last_key) } /* }}} */ +/* {{{ proto array array_first_key(array input) + Returns last key in the array */ +PHP_FUNCTION(array_first_key) +{ + HashTable *array; + char *string_key; + uint string_key_len; + ulong num_key; + Bucket *bucket; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) { + return; + } + + bucket = array->pListHead; + if (bucket) { + string_key_len = bucket->nKeyLength; + /* if the key length > 0, it's a hashed string, else numeric key */ + if (string_key_len > 0) { + string_key = bucket->arKey; + /* length - 1 because null-terminated... */ + RETVAL_STRINGL(string_key, string_key_len - 1, 1); + } else { + num_key = bucket->h; + RETVAL_LONG(num_key); + } + } else { + return; /* empty array */ + } +} +/* }}} */ + /* {{{ proto mixed min(mixed arg1 [, mixed arg2 [, mixed ...]]) Return the lowest value in an array or a series of arguments */ PHP_FUNCTION(min) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 54d2571d28682..bbce06722bbb5 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -300,6 +300,10 @@ ZEND_BEGIN_ARG_INFO(arginfo_array_last_key, 0) ZEND_ARG_INFO(0, arg) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_array_first_key, 0) + ZEND_ARG_INFO(0, arg) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_min, 0, 0, 1) ZEND_ARG_INFO(0, arg1) ZEND_ARG_INFO(0, arg2) @@ -3298,6 +3302,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(current, arginfo_current) PHP_FE(key, arginfo_key) PHP_FE(array_last_key, arginfo_array_last_key) + PHP_FE(array_first_key, arginfo_array_first_key) PHP_FE(min, arginfo_min) PHP_FE(max, arginfo_max) PHP_FE(in_array, arginfo_in_array) diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index 771cd1d74007a..1bbd3876d830a 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -48,6 +48,7 @@ PHP_FUNCTION(reset); PHP_FUNCTION(current); PHP_FUNCTION(key); PHP_FUNCTION(array_last_key); +PHP_FUNCTION(array_first_key); PHP_FUNCTION(min); PHP_FUNCTION(max); PHP_FUNCTION(in_array); diff --git a/ext/standard/tests/array/array_first_key.phpt b/ext/standard/tests/array/array_first_key.phpt new file mode 100644 index 0000000000000..4afd02a1d68ef --- /dev/null +++ b/ext/standard/tests/array/array_first_key.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test array_first_key() function +--FILE-- + 7]; +var_dump( array_first_key($a) ); + +$a = [1, 2, 3, 4, 5, 6]; +var_dump( array_first_key($a) ); + +echo "Done\n"; +?> +--EXPECTF-- +NULL +int(0) +string(6) "foobar" +int(0) +Done