PHP 获取数组中最后插入项的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3275082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:11:59  来源:igfitidea点击:

PHP get index of last inserted item in array

phparraysindexing

提问by Christian

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)-1is flawed.

简而言之,流行的解决方法count($array)-1是有缺陷的。

回答by romaninsh

Here is a linear(fastest) solution:

这是一个线性最快)解决方案:

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

回答by VolkerK

You can use key($a)together with end($a)

您可以将key($a)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

回答by Sam152

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:

您可以使用end()函数获取数组中的最后一个元素,并使用array_keys()返回数组键的数组。令人困惑。在实践中,它是这样工作的:

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

Credit goes to hollsk in the comments.

在评论中归功于霍尔斯克。

回答by Pascal MARTIN

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.

因此,对于自动生成的索引,使用类似的方法max(array_keys($a))应该可行。

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...


但请注意,这不适用于您指定索引的情况...

回答by Christian

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

呸,看来我自己找到了答案:

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

回答by Christoph Diegelmann

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

在数字键从 0 到 n-1 的数组上,我总是使用:

$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.

我猜它不会变得更快或更简单。如果有人有类似的东西适用于其他阵列,请告诉我。