Find the first element of an array in PHP
This post will discuss how to get the first element of an array in PHP without deleting it.
1. Using reset() function
The key() function returns the key of the array element that’s currently being pointed to by the internal pointer. You can set the array’s internal pointer to its first element using the reset() function, which also returns the value of the first element.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $arr = array('One' => 'a', 'Two' => 'b', 'Three' => 'c'); $first_value = reset($arr); $first_key = key($arr); echo $first_key . ' => ' . $first_value; /* Output: One => a */ ?> |
The array’s internal pointer is initialized to the first element of the array. If the array has never been repositioned before, the internal pointer would be pointing to the first element. In such cases, you can directly invoke the current() function to get the value of the first element.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $arr = array('One' => 'a', 'Two' => 'b', 'Three' => 'c'); $first_value = current($arr); $first_key = key($arr); echo $first_key . ' => ' . $first_value; /* Output: One => a */ ?> |
2. Using array_key_first() function
The recommended way to get the first key of an array is using array_key_first() function. This function is available with PHP 7.3.0 and above, outperforms reset() function in terms of performance, and avoids any side effects such as losing the current array pointer position.
Since array_key_first() function returns the “key” present at the beginning of the array, you can use the array’s access notation to get the associated value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $arr = array('One' => 'a', 'Two' => 'b', 'Three' => 'c'); $first_key = array_key_first($arr); if ($first_key !== null) { $first_value = $arr[$first_key]; echo $first_key . ' => ' . $first_value; } /* Output: One => a */ ?> |
3. Using array_shift() function
For PHP <= 7.2, you can use the array_slice() function to get the first element of an array. It accepts the offset and length parameters, which represent the starting position, and extracts a slice from the array of the specified length.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $arr = array('One' => 'a', 'Two' => 'b', 'Three' => 'c'); $slice = array_slice($arr, 0, 1); print_r($slice); /* Output: Array ( [One] => a ) */ ?> |
The array_slice() function acts as an efficient alternative to the reset() function. However, it returns an array. To get the value of the first element, you can call the array_shift() function on the slice.
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $arr = array('One' => 'a', 'Two' => 'b', 'Three' => 'c'); $slice = array_slice($arr, 0, 1); $first_value = array_shift($slice); echo $first_value; /* Output: a */ ?> |
4. Using Array Access Notation
You can also access the first element of an array using array access notation. However, this only works with numerical arrays and fails for associative arrays.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $array = [1, 2, 3, 4, 5]; if (sizeof($arr) > 0) { $first = $array[0]; echo $first; } /* Output: 1 */ ?> |
That’s all about finding the first element of an array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)