Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get numeric key of new pushed item in PHP?

Tags:

arrays

php

$arr[] = $new_item;

Is it possible to get the newly pushed item programmatically?

Note that it's not necessary count($arr)-1:

$arr[1]=2;
$arr[] = $new_item;

In the above case,it's 2

like image 727
user198729 Avatar asked Feb 04 '10 07:02

user198729


2 Answers

end() do the job , to return the value ,

if its help to you ,

you can use key() after to petch the key.

after i wrote the answer , i see function in this link :

http://www.php.net/manual/en/function.end.php

function endKey($array){
 end($array);
 return key($array);
}
like image 107
Haim Evgi Avatar answered Sep 19 '22 14:09

Haim Evgi


max(array_keys($array)) should do the trick

like image 21
rossipedia Avatar answered Sep 21 '22 14:09

rossipedia