PHP gets index of last inserted element into array

It is as simple as the title sounds; I need to get the index / key of the last element inserted. Why is it difficult? See the following two code examples:

$a=array(); echo 'res='.($a[]='aaa').' - '.(count($a)-1).'<br>'; echo 'res='.($a[]='bbb').' - '.(count($a)-1).'<br>'; echo 'res='.($a[]='aaa').' - '.(count($a)-1).'<br>'; die('<pre>'.print_r($a,true).'</pre>'); 

Writes:

 res=aaa - 0 res=bbb - 1 res=aaa - 2 Array ( [0] => aaa [1] => bbb [2] => aaa ) 

Of course this works fine, but look at this:

 $a=array(); echo 'res='.($a[]='aaa').' - '.(count($a)-1).'<br>'; echo 'res='.($a[2]='bbb').' - '.(count($a)-1).'<br>'; echo 'res='.($a[]='aaa').' - '.(count($a)-1).'<br>'; die('<pre>'.print_r($a,true).'</pre>'); 

Writes:

 res=aaa - 0 res=bbb - 1 <- wrong! res=aaa - 2 <- wrong! Array ( [0] => aaa [2] => bbb <- real key [3] => aaa <- real key ) 

In short, the popular workaround count($array)-1 wrong.

+46
arrays php indexing
Jul 18 '10 at 10:05
source share
7 answers

Here is a linear ( fastest ) solution:

 end($a); $last_id=key($a); 
+61
Apr 6 2018-12-12T00:
source share

You can use the key ($ a) along with end ($ a)

 $a=array(); $a[]='aaa'; foo($a); $a[3]='bbb'; foo($a); $a['foo']='ccc'; foo($a); $a[]='ddd'; foo($a); function foo(array $a) { end($a); echo 'count: ', count($a), ' last key: ', key($a), "\n"; } 

prints

 count: 1 last key: 0 count: 2 last key: 3 count: 3 last key: foo count: 4 last key: 4 
+19
Jul 18 '10 at 10:25
source share

You can use the end () function to get the last element in the array, and array_keys () to return the array of array keys. Mixing. In practice, it works as follows:

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

The loan is sent to Kholsk in the comments.

+18
Jul 18 '10 at 10:10
source share

If you only work with numeric indexes for your array, the last automatically generated index will always be the largest array of the array array.

So, for automatically generated indexes, using max(array_keys($a)) should work.

For example, this:

 $a=array(); echo 'res='.($a[]='aaa').' - '.(max(array_keys($a))).'<br>'; echo 'res='.($a[2]='bbb').' - '.(max(array_keys($a))).'<br>'; echo 'res='.($a[]='aaa').' - '.(max(array_keys($a))).'<br>'; die('<pre>'.print_r($a,true).'</pre>'); 

You'll get:

 res=aaa - 0 res=bbb - 2 res=aaa - 3 Array ( [0] => aaa [2] => bbb [3] => aaa ) 


But keep in mind that this will not work for situations where you specified an index ...

+2
Jul 18 '10 at 10:11
source share

Ba, it seems I found the answer myself:

 $last_id = array_pop(array_keys($a)); 
+1
Jul 18 '10 at 10:10
source share

On arrays with numeric keys from 0 to n-1, I always use:

 $key = array_push($array, $value)-1; 

It does not work faster or easier. If someone got something like this that works for other arrays, please let me know.

+1
Jan 22 '14 at 23:04
source share

If you want the index to just get the last element inserted.

Using the function, just use:

 return $array[] = $value; 
0
Mar 22 '17 at 17:07
source share



All Articles