note 129096 added to function.array-key-first
| From: | MaxiCom dot Developpement at gmail dot com | Date: | Sat, 09 Dec 2023 10:41:05 +0000 |
| Subject: | note 129096 added to function.array-key-first | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221050@lists.php.net to get a copy of this message | ||
A polyfill serves the purpose of retroactively incorporating new features from PHP releases into
older PHP versions, ensuring API compatibility.
In PHP 7.3.0, the array_key_first() function was introduced, demonstrated in the following example:
<?php
$array = [
'first_key' => 'first_value',
'second_key' => 'second_value',
];
var_dump(array_key_first($array));
?>
The provided polyfill in this documentation allows the convenient use of array_key_first() with API
compatibility in PHP versions preceding PHP 7.3.0, where the function was not implemented:
<?php
if (!function_exists('array_key_first')) {
function array_key_first(array $arr) {
foreach ($arr as $key => $unused) {
return $key;
}
return null;
}
}
$array = [
'first_key' => 'first_value',
'second_key' => 'second_value',
];
var_dump(array_key_first($array));
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 2a02:8440:d109:5ad1:1b18:f4a7:568c:4465)
----
Manual Page -- https://php.net/manual/en/function.array-key-first.php
Edit -- https://main.php.net/note/edit/129096
Del: integrated -- https://main.php.net/note/delete/129096/integrated
Del: useless -- https://main.php.net/note/delete/129096/useless
Del: bad code -- https://main.php.net/note/delete/129096/bad+code
Del: spam -- https://main.php.net/note/delete/129096/spam
Del: non-english -- https://main.php.net/note/delete/129096/non-english
Del: in docs -- https://main.php.net/note/delete/129096/in+docs
Del: other reasons-- https://main.php.net/note/delete/129096
Reject -- https://main.php.net/note/reject/129096
Search -- https://main.php.net/manage/user-notes.php