If I have:
$array = array( 'one' =>'value', 'two' => 'value2' );
how do I get the string one back from $array[1] ?
one
$array[1]
You don't. Your array doesn't have a key [1]. You could:
[1]
Make a new array, which contains the keys:
$newArray = array_keys($array); echo $newArray[0];
But the value "one" is at $newArray[0], not [1]. A shortcut would be:
$newArray[0]
echo current(array_keys($array));
Get the first key of the array:
reset($array); echo key($array);
Get the key corresponding to the value "value":
echo array_search('value', $array);
This all depends on what it is exactly you want to do. The fact is, [1] doesn't correspond to "one" any which way you turn it.
2.1m questions
2.1m answers
60 comments
57.0k users