Skip to content
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

[RFC] Implement array_first() and array_last() #18210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4511,6 +4511,32 @@ PHP_FUNCTION(array_key_last)
}
/* }}} */

PHP_FUNCTION(array_first)
{
HashTable *array;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_HT(array)
ZEND_PARSE_PARAMETERS_END();

ZEND_HASH_FOREACH_VAL(array, zval *zv) {
RETURN_COPY_DEREF(zv);
} ZEND_HASH_FOREACH_END();
}

PHP_FUNCTION(array_last)
{
HashTable *array;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_HT(array)
ZEND_PARSE_PARAMETERS_END();

ZEND_HASH_REVERSE_FOREACH_VAL(array, zval *zv) {
RETURN_COPY_DEREF(zv);
} ZEND_HASH_FOREACH_END();
}

/* {{{ Return just the values from the input array */
PHP_FUNCTION(array_values)
{
Expand Down
10 changes: 10 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,16 @@ function array_key_first(array $array): int|string|null {}
*/
function array_key_last(array $array): int|string|null {}

/**
* @compile-time-eval
*/
function array_first(array $array): mixed {}

/**
* @compile-time-eval
*/
function array_last(array $array): mixed {}

/**
* @return array<int, mixed|ref>
* @compile-time-eval
Expand Down
12 changes: 11 additions & 1 deletion ext/standard/basic_functions_arginfo.h

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

53 changes: 53 additions & 0 deletions ext/standard/tests/array/array_first_last.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
array_first()/array_last()
--FILE--
<?php
$str = "hello world";

$test_cases = [
["single element"],
[&$str, 1],
[1, &$str],
[1 => 1, 0 => 0, 3 => 3, 2 => 2],
[100 => []],
[new stdClass, false],
[true, new stdClass],
];

foreach ($test_cases as $test_case) {
// Output the checked values
echo "--- Testing: ", json_encode($test_case), " ---\n";
echo "First: ", json_encode(array_first($test_case)), "\n";
echo "Last: ", json_encode(array_last($test_case)), "\n";

// Sanity check consistency with array_key_first()/array_key_last()
if (array_first($test_case) !== $test_case[array_key_first($test_case)]) {
throw new Error("Key first and value first inconsistency");
}
if (array_last($test_case) !== $test_case[array_key_last($test_case)]) {
throw new Error("Key last and value last inconsistency");
}
}
?>
--EXPECT--
--- Testing: ["single element"] ---
First: "single element"
Last: "single element"
--- Testing: ["hello world",1] ---
First: "hello world"
Last: 1
--- Testing: [1,"hello world"] ---
First: 1
Last: "hello world"
--- Testing: {"1":1,"0":0,"3":3,"2":2} ---
First: 1
Last: 2
--- Testing: {"100":[]} ---
First: []
Last: []
--- Testing: [{},false] ---
First: {}
Last: false
--- Testing: [true,{}] ---
First: true
Last: {}
20 changes: 20 additions & 0 deletions ext/standard/tests/array/array_first_last_errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
array_first()/array_last() error cases
--FILE--
<?php
var_dump(array_first([]));
var_dump(array_last([]));

$array = [1, 2, 3];
unset($array[0]);
unset($array[1]);
unset($array[2]);

var_dump(array_first($array));
var_dump(array_last($array));
?>
--EXPECT--
NULL
NULL
NULL
NULL
Loading