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

mimi0110's avatar

Get array keys php/ laravel

I have an array:

array:2 [
  "date" => "2020-01-19"
  "user_id" => 123
]

I would like to get the array keys only, like that:

['date', `user_id`]

I tried to use array_keys php but it's not my expected It return :

array:2 [
  0 => "date"
  1 => "user_id"
]

I also tried to use collection on laravel

$collection = collect(["date" => "2020-01-19",
  "user_id" => 123
  ]);

$keys = $collection->keys();

return $keys->all();

Result:

array:2 [▼
  0 => "date"
  1 => "user_id"
]

althought the laravel document wrote an example:

$collection = collect([
    'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
    'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
]);

$keys = $collection->keys();

$keys->all();

// ['prod-100', 'prod-200']

Can anybody help to exlain to me how to get

['date', `user_id`]

any response is appreciated !

0 likes
7 replies
ismaile's avatar
ismaile
Best Answer
Level 30

Actually, you had the right result, it's just formatted in a different way.

array:2 [▼
  0 => "date"
  1 => "user_id"
]

is equivalent to:

[
  "date",
  "user_id"
]
2 likes
ismaile's avatar

I've just installed it and tested it a bit, I love it. Thanks a lot :)

mimi0110's avatar

Thank ! That may my stupid question :(

ismaile's avatar

You're welcome, the display might be confusing at the beginning :)

Please or to participate in this conversation.