PHP get index of last inserted item in array

前端 未结 6 1886
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 05:40

It\'s as easy as the title sounds; I need to get the index/key of the last inserted item. Why is this difficult? See the following two code samples:

$a=array         


        
6条回答
  •  失恋的感觉
    2020-11-29 06:14

    You can use the end() function to get the last element in an array, and array_keys() to return an array of the array-keys. Confusing. In practice, it works like this:

    $key = end(array_keys($array));
    

    Credit goes to hollsk in the comments.

提交回复
热议问题