How can I get the last key of an array?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A solution would be to use a combination of
endandkey(quoting) :end()advances array ‘s internal pointer to the last element, and returns its value.key()returns the index element of the current array position.So, a portion of code such as this one should do the trick :
Will output :
i.e. the key of the last element of my array.
After this has been done the array’s internal pointer will be at the end of the array. As pointed out in the comments, you may want to run
reset()on the array to bring the pointer back to the beginning of the array.