php - Returning array key when using array[] = -
what simple method return array key when using array[] 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 different solution?
conclusion
a combination of end()
, key()
best in general allows associative if array uses numerical keys, count()-1
seems simplest , fast. added other linked question.
$array[] = 'hello world'; end($array); // set internal pointer end of array $key = key($array); // key of element internal pointer pointing @ return $key;
Comments
Post a Comment