获取 PHP 数组中的最后一个键值对

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3801891/
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 11:10:09  来源:igfitidea点击:

Get last key-value pair in PHP array

phparrays

提问by greenbandit

I have an array that is structured like this:

我有一个结构如下的数组:

[33] => Array
    (
        [time] => 1285571561
        [user] => test0
    )

[34] => Array
    (
        [time] => 1285571659
        [user] => test1
    )

[35] => Array
    (
        [time] => 1285571682
        [user] => test2
    )

How can I get the last value in the array, but maintaining the index [35]?

如何获取数组中的最后一个值,但保持索引 [35]?

The outcome that I am looking for is this:

我正在寻找的结果是这样的:

[35] => Array
    (
        [time] => 1285571682
        [user] => test2
    )

回答by Alex Pliutau

try to use

尝试使用

end($array);

回答by salathe

$last = array_slice($array, -1, 1, true);

See http://php.net/array_slicefor details on what the arguments mean.

有关参数含义的详细信息,请参阅http://php.net/array_slice

P.S. Unlike the other answers, this one actually does what you want. :-)

PS 与其他答案不同,这个答案实际上可以满足您的需求。:-)

回答by Gumbo

You can use endto advance the internal pointer to the end or array_sliceto get an array only containing the last element:

您可以使用end将内部指针推进到末尾或array_slice获取仅包含最后一个元素的数组:

$last = end($arr);
$last = current(array_slice($arr, -1));

回答by Govinda Yadav

If you have an array

如果你有一个数组

$last_element = array_pop(array);

回答by Ali

Example 1:

示例 1:

$arr = array("a"=>"a", "5"=>"b", "c", "key"=>"d", "lastkey"=>"e");
print_r(end($arr));

Output= e

输出= e



Example 2:

示例 2:

ARRAY withoutkey(s)

没有键的数组

$arr = array("a", "b", "c", "d", "e");
print_r(array_slice($arr, -1, 1, true));
// output is = array( [4] => e ) 


Example 3:

示例 3:

ARRAY withkey(s)

键的数组

$arr = array("a"=>"a", "5"=>"b", "c", "key"=>"d", "lastkey"=>"e");
print_r(array_slice($arr, -1, 1, true));
// output is = array ( [lastkey] => e ) 


Example 4:

示例 4:

If your array keys like : [0] [1] [2] [3] [4] ... etc. You can use this:

如果你的数组键像:[0] [1] [2] [3] [4] ...等,你可以使用这个:

$arr = array("a","b","c","d","e");
$lastindex = count($arr)-1;
print_r($lastindex);

Output= 4

输出= 4



Example 5:But if you are not sure!

示例 5:但如果您不确定!

$arr = array("a"=>"a", "5"=>"b", "c", "key"=>"d", "lastkey"=>"e");
$ar_k = array_keys($arr);
$lastindex = $ar_k [ count($ar_k) - 1 ];
print_r($lastindex);

Output= lastkey

输出= 最后一个键



Resources:

资源:

  1. https://php.net/array_slice
  2. https://www.php.net/manual/en/function.array-keys.php
  3. https://www.php.net/manual/en/function.count.php
  4. https://www.php.net/manual/en/function.end.php
  1. https://php.net/array_slice
  2. https://www.php.net/manual/en/function.array-keys.php
  3. https://www.php.net/manual/en/function.count.php
  4. https://www.php.net/manual/en/function.end.php

回答by ipalaus

Like said Gumbo,

就像秋葵说的,

<?php

$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry

?>

回答by Yas

Another solution cold be:

另一个解决方案冷是:

$value = $arr[count($arr) - 1];

The above will count the amount of array values, substract 1 and then return the value.

以上将计算数组值的数量,减去1然后返回值。

Note: This can only be used if your array keys are numeric.

注意:这只能在您的数组键是数字时使用。

回答by jmary

As the key is needed, the accepted solution doesn't work.

由于需要密钥,接受的解决方案不起作用。

This:

这个:

end($array);
return array(key($array) => array_pop($array));

will return exactly as the example in the question.

将完全按照问题中的示例返回。

回答by voodoo417

"SPL-way":

"SPL-way"

$splArray = SplFixedArray::fromArray($array);
$last_item_with_preserved_index[$splArray->getSize()-1] = $splArray->offsetGet($splArray->getSize()-1);

Read more about SplFixedArrayand why it's in some cases ( especially with big-index sizes array-data) more preferable than basic arrayhere => The SplFixedArray class.

在这里阅读更多关于SplFixedArray它以及为什么它在某些情况下(尤其是大索引大小的数组数据)比基本更可取的array地方 => SplFixedArray 类