PHP get index of last inserted item in array

PhpArraysIndexing

Php Problem Overview


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();
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
)

Sure, that seems to work fine, but see 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
)

So in short, the popular workaround count($array)-1 is flawed.

Php Solutions


Solution 1 - Php

Here is a linear (fastest) solution:

end($a);
$last_id=key($a);

Solution 2 - Php

You can use http://docs.php.net/key">key($a)</a> together with http://docs.php.net/end">end($a)</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

Solution 3 - Php

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.

Solution 4 - Php

If you are only working with numerical indexes for your array, the last auto-generated index will always be the biggest array key of the array.

So, for auto-generated indexes, using something like 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>');

Would get you :

res=aaa - 0
res=bbb - 2
res=aaa - 3

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


But note that this will not work for situations when you are the one specifying the index...

Solution 5 - Php

Bah, looks like I've found the answer by myself:

$last_id = array_pop(array_keys($a));

Solution 6 - Php

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

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

It doesn't get any faster or simpler I guess. If someone got something like this that works for other arrays please let me know.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristianView Question on Stackoverflow
Solution 1 - PhpromaninshView Answer on Stackoverflow
Solution 2 - PhpVolkerKView Answer on Stackoverflow
Solution 3 - PhpSam152View Answer on Stackoverflow
Solution 4 - PhpPascal MARTINView Answer on Stackoverflow
Solution 5 - PhpChristianView Answer on Stackoverflow
Solution 6 - PhpChristoph DiegelmannView Answer on Stackoverflow