How to get first key of an Array in PHP

In this example shows you how to get first key of an array by using key and reset built-in php functions.

example.php
<?php

$myArray = array(
   'first' => 'This is first value',
   'second' => 'This is second value',
   'third' => 'This is third value',
   'fourth' => 'This is fourth value',
);

// get the first key of the array
reset($myArray); 
$firstKey = key($myArray);

echo $firstKey; // first

In the above code we firstly use reset function to reset the array’s internal pointer to the first element and then use key function to return the element key from the current internal pointer position.

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x