What is a simple method to return the array key when using array[] to add new values.
For example:
$array[] = 'Hello World';
return $Key_of_Hello_World;
The method I'm thinking of involves:
$key = count($array)-1;
Is there a different solution?
Conclusion
A combination of end()
and key()
is the best in general as it allows for associative but if your array only uses numerical keys, count()-1
seems to be the simplest and just as fast. I added this to the other linked question.
end($array); $key = key($array);
– Sean Mar 10 at 5:16