Search code examples
phparraysarraylistkeyelement

php can not get array value from an array


I have an array:

Array
(
[ZmlkPTQ3NyZ1aWQ9MTA4NDQmdXNlcl9sb2dpbj52
] => 
[format] => html
[Itemid] => 971
[option] => com_ira_financial_forms
[view] => add_self_directed_ira
)

And I would like to get the first value in the array:

ZmlkPTQ3NyZ1aWQ9MTA4NDQmdXNlcl9sb2dpbj52

I have tried each of the following:

  • array_values($_array)[0];
  • $data[0];
  • array_keys($data)[0];

But none of them work, and I instead get an empty string. I need to get the first element or key of the array.


Solution

  • you can use array_key_first(your_array)

    Get the first key of the given array without affecting the internal array pointer.