Page 1 of 1
array - find last inserted key
Posted: Wed Mar 29, 2006 3:33 am
by asgerhallas
I have this short script:
Code: Select all
$a[]="one";
$a[]="two";
$a[]="three";
What's the best way to find key of the last inserted item?
/Asger
Posted: Wed Mar 29, 2006 3:37 am
by s.dot
array_keys()
usage:
Code: Select all
$key = array_keys($array,"value_to_find_keys_for");
print_r($key);
Posted: Wed Mar 29, 2006 3:50 am
by asgerhallas
Ok, thanks!
But then sometimes it can be something like this:
Where all the values are the same, so I can't use the search_value. What do I do then?
I've tried this:
But I can't figure if it will always give me the latest inserted key? What if I sort it for example?
Posted: Wed Mar 29, 2006 3:59 am
by s.dot
hmm
im sure there's a better way than this
but this will work
Code: Select all
foreach($array AS $k=>$v){
if(!empty($v)){
$lastKey = $k;
}
}
echo $lastKey;
Posted: Wed Mar 29, 2006 4:15 am
by JayBird
Posted: Wed Mar 29, 2006 4:27 am
by asgerhallas
That works. Thanks a lot!