Alexandre Elshobokshy
Alexandre Elshobokshy

Reputation: 10922

Get values of array keys

I've got an array like the following :

$arr = array('Test' => 'stuff');

Getting it like this :

$arr_keys = array_keys($arr);

Gives me the following schema :

array(0 => 'Test');

Now I want to remove the keys on that array, but doing this doesn't work :

array_values($arr_keys)

This gives me the same key => value array, how to remove the keys and only get the desired result which is :

array('Test');

Upvotes: 0

Views: 64

Answers (1)

Qirel
Qirel

Reputation: 26450

Your result of array(0 => 'Test'); is exactly the desired output you expect to get. All array-elements must have a unique index to it - and if you don't provide one, PHP will create one for you.

When you do

$array = array('Test');

You create an array with a singular element in it - but since you did not specify a key to this value, it will be auto-generated by PHP, numerically starting from zero. You can verify this by doing

print_r($array);

..which will yield the same result as your $arr_keys = array_keys($arr);

See this live demo.

Upvotes: 2

Related Questions