Skip to content

Added array_last_key() function, returns key of last array item #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
64 changes: 64 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,70 @@ PHP_FUNCTION(key)
}
/* }}} */

/* {{{ proto array array_last_key(array input)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a '$' in parameter name :)

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could use RETURN_STRINGL() to return early.
so does the later RETVAL_LONG => RETURN_LONG
:)

} else {
num_key = bucket->h;
RETVAL_LONG(num_key);
}
} else {
return; /* empty array */
}
}
/* }}} */

/* {{{ 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)
Expand Down
10 changes: 10 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ 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(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)
Expand Down Expand Up @@ -3293,6 +3301,8 @@ 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(array_first_key, arginfo_array_first_key)
PHP_FE(min, arginfo_min)
PHP_FE(max, arginfo_max)
PHP_FE(in_array, arginfo_in_array)
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/php_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ PHP_FUNCTION(next);
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);
Expand Down
24 changes: 24 additions & 0 deletions ext/standard/tests/array/array_first_key.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Test array_first_key() function
--FILE--
<?php
$a = [];
var_dump( array_first_key($a) );

$a[] = 7; /* it shouldn't matter what the value is, so I'm using 7 */
var_dump( array_first_key($a) );

$a = ["foobar" => 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
24 changes: 24 additions & 0 deletions ext/standard/tests/array/array_last_key.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Test array_last_key() function
--FILE--
<?php
$a = [];
var_dump( array_last_key($a) );

$a[] = 7; /* it shouldn't matter what the value is, so I'm using 7 */
var_dump( array_last_key($a) );

$a[1] = 7;
var_dump( array_last_key($a) );

$a = ["foobar" => 7];
var_dump( array_last_key($a) );

echo "Done\n";
?>
--EXPECTF--
NULL
int(0)
int(1)
string(6) "foobar"
Done