<p>Is there a better way to do the following:</p>

<pre><code>$array = array('test1', 'test2', 'test3', 'test4', 'test5');
// do a bunch of other stuff, probably a loop
$array[] = 'test6';
end($array);
echo key($array); // gives me 6
</code></pre>

<p>This will give the key of the most recently add array element.</p>

<p>Is there a better way to do this?</p>

You could also do:
```
```
$end = end(array_keys($array));
```
```
But I think your way makes it clear what you want to do, so you could whip something up like:
```
```
function array_last_key($array) {
    end($array);
    return key($array);
}
```
```
That's about it.