How to get the first element of an array in PHP? (detailed guide)
Hello everyone! I'm a beginner in PHP and I've encountered what seems like a simple task, but I can't find a universal solution.
I have several arrays that I work with. Sometimes they are regular indexed arrays (with numeric keys starting from 0), sometimes they are associative arrays (with string keys). I need to get the very first element from any such array, regardless of what keys it has.
Here are examples of my arrays:
```php
// Indexed array
$fruits = ['apple', 'banana', 'orange'];
// Associative array
$user = ['name' => 'Ivan', 'age' => 30, 'city' => 'Moscow'];
// Array with arbitrary numeric keys
$numbers = [5 => 'five', 10 => 'ten', 3 => 'three'];
```
I tried using `$array[0]`, but it only works for the first example.
No answers yet.
Log in to leave an answer