<?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> <refentry xml:id="function.array-key-first" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> <refnamediv> <refname>array_key_first</refname> <refpurpose>Gets the first key of an array</refpurpose> </refnamediv> <refsect1 role="description"> &reftitle.description; <methodsynopsis> <type class="union"><type>int</type><type>string</type><type>null</type></type><methodname>array_key_first</methodname> <methodparam><type>array</type><parameter>array</parameter></methodparam> </methodsynopsis> <para> Get the first key of the given <parameter>array</parameter> without affecting the internal array pointer. </para> </refsect1> <refsect1 role="parameters"> &reftitle.parameters; <variablelist> <varlistentry> <term><parameter>array</parameter></term> <listitem> <para> An array. </para> </listitem> </varlistentry> </variablelist> </refsect1> <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> Returns the first key of <parameter>array</parameter> if the array is not empty; &null; otherwise. </para> </refsect1> <refsect1 role="examples"> &reftitle.examples; <para> <example xml:id="array_key_first.example.basic"> <title>Basic <function>array_key_first</function> Usage</title> <programlisting role="php"> <![CDATA[ <?php $array = ['a' => 1, 'b' => 2, 'c' => 3]; $firstKey = array_key_first($array); var_dump($firstKey); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ string(1) "a" ]]> </screen> </example> </para> </refsect1> <refsect1 role="notes"> &reftitle.notes; <tip> <simpara> There are several ways to provide this functionality for versions prior to PHP 7.3.0. It is possible to use <function>array_keys</function>, but that may be rather inefficient. It is also possible to use <function>reset</function> and <function>key</function>, but that may change the internal array pointer. An efficient solution, which does not change the internal array pointer, written as polyfill: </simpara> <informalexample> <programlisting role="php"> <![CDATA[ <?php if (!function_exists('array_key_first')) { function array_key_first(array $arr) { foreach($arr as $key => $unused) { return $key; } return NULL; } } ?> ]]> </programlisting> </informalexample> </tip> </refsect1> <refsect1 role="seealso"> &reftitle.seealso; <simplelist> <member><function>array_key_last</function></member> <member><function>reset</function></member> </simplelist> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"~/.phpdoc/manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->