Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

d3s1gn3r's avatar

How can i get only the first element of an array ?

Hi, this is my first post and i am new in Laravel.

In my laravel app this code generate a list of elements: array_get($cars, 'models')

How can i change it so i can get only the first element from the list? i tried array_first but it doesn't work.

Any help would be very much appreciated.

0 likes
5 replies
Brandon Eichhorn's avatar

Can you show us what $cars holds as a value?

For example, if this is your array:

$cars = ['bmw' => ['gas' => 'x6']];

Now you can access the value that 'gas' holds using array_get($cars, 'bmw.gas)

This will return: 'x6'.

Snapey's avatar

or perhaps you want the first element of every item in the collection, but aren't explaining it

$cars->pluck('model');

1 like

Please or to participate in this conversation.