如何在数组中获取最后一个键?

时间:2022-10-24 17:14:08

How can I get the last key of an array?

如何获得数组的最后一个键?

13 个解决方案

#1


521  

A solution would be to use a combination of end and key (quoting) :

一个解决方案是使用end和key的组合(引用):

  • end() advances array 's internal pointer to the last element, and returns its value.
  • end()将数组的内部指针移到最后一个元素,并返回其值。
  • key() returns the index element of the current array position.
  • 键()返回当前数组位置的索引元素。

So, a portion of code such as this one should do the trick :

所以,像这样的一部分代码应该可以做到:

$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

end($array);         // move the internal pointer to the end of the array
$key = key($array);  // fetches the key of the element pointed to by the internal pointer

var_dump($key);

Will output :

将输出:

string 'last' (length=4)

i.e. the key of the last element of my array.

即数组最后一个元素的键。

#2


66  

Although end() seems to be the easiest, it's not the fastest.

虽然end()看起来是最容易的,但它不是最快的。

The faster, and much stronger alternative is array_slice()

array_slice()是更快、更强的替代方法

Strike-out by editor: This claim has not been substantiated and contrary evidence has been mentioned in the comments. If there is any shred of truth to this poster's assertions about speed/efficiency then a benchmark test must be provided.

编辑:这一说法没有得到证实,而且在评论中也提到了相反的证据。如果这张海报关于速度/效率的断言有任何真相,那么就必须提供基准测试。

$last_key = key( array_slice( $array, -1, 1, TRUE ) );

#3


43  

I prefer

我更喜欢

end(array_keys($myarr))

#4


26  

Just use : echo $array[count($array) - 1];

使用:echo $array[count($array) - 1];

#5


19  

Dont know if this is going to be faster or not, but it seems easier to do it this way, and you avoid the error by not passing in a function to end()...

不知道这是否会更快,但这样做似乎更容易,并且避免了通过函数结束()来避免错误……

it just needed a variable... not a big deal to write one more line of code, then unset it if you needed to.

它只是需要一个变量…不需要再写一行代码,如果需要的话就取消设置。

$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

$keys = array_keys($array);
$last = end($keys);

#6


8  

Try using array_pop and array_keys function as follows:

尝试使用array_pop和array_keys函数如下:

<?php

$array = array(
    'one' => 1,
    'two' => 2,
    'three' => 3
);

echo array_pop(array_keys($array)); // prints three

?>

#7


3  

I would also like to offer an alternative solution to this problem.

我还想为这个问题提供另一种解决方案。

Assuming all your keys are numeric without any gaps, my preferred method is to count the array then minus 1 from that value (to account for the fact that array keys start at 0.

假设所有的键都是数字而没有任何间隔,我首选的方法是计算数组的值,然后从该值中减去1(来说明数组键从0开始的事实)。

$array = array(0=>'dog', 1=>'cat');

$lastKey = count($array)-1;
$lastKeyValue = $array[$lastKey];

var_dump($lastKey);
print_r($lastKeyValue);

This would give you:

这将给你:

int(1) cat

int(1)猫

#8


3  

You can use this:

您可以使用:

$array = array("one" => "apple", "two" => "orange", "three" => "pear");
end($array); 
echo key($array);

Another Solution is to create a function and use it:

另一个解决方案是创建一个函数并使用它:

function endKey($array){
end($array);
return key($array);
}

$array = array("one" => "apple", "two" => "orange", "three" => "pear");
echo endKey($array);

#9


3  

I just took the helper-function from Xander and improved it with the answers before:

我只是从Xander中提取了helper函数,并在之前对它进行了改进:

function last($array){
  $keys = array_keys($array);
  return end($keys);
}


$arr = array("one" => "apple", "two" => "orange", "three" => "pear");    
echo last($arr);
echo $arr(last($arr));

#10


2  

$arr = array('key1'=>'value1','key2'=>'value2','key3'=>'value3');
list($last_key) = each(array_reverse($arr));
print $last_key;
// key3

#11


-2  

$array = array(
    'something' => array(1,2,3),
    'somethingelse' => array(1,2,3,4)
);

$last_value = end($array);
$last_key = key($array); // 'somethingelse'

This works because PHP moves it's array pointer internally for $array

这是可行的,因为PHP将它的数组指针移动到$array内部。

#12


-3  

Like this:

是这样的:

$keys = array_keys($array);
$lastKey = $keys[sizeof($array)-1];

#13


-3  

It should be something like this...

应该是这样的……

$latest_kid = end(array_reverse(array_keys($my_array)));

Cause it reverse the array when we call array_keys.

因为当我们调用array_keys时它会反转数组。

#1


521  

A solution would be to use a combination of end and key (quoting) :

一个解决方案是使用end和key的组合(引用):

  • end() advances array 's internal pointer to the last element, and returns its value.
  • end()将数组的内部指针移到最后一个元素,并返回其值。
  • key() returns the index element of the current array position.
  • 键()返回当前数组位置的索引元素。

So, a portion of code such as this one should do the trick :

所以,像这样的一部分代码应该可以做到:

$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

end($array);         // move the internal pointer to the end of the array
$key = key($array);  // fetches the key of the element pointed to by the internal pointer

var_dump($key);

Will output :

将输出:

string 'last' (length=4)

i.e. the key of the last element of my array.

即数组最后一个元素的键。

#2


66  

Although end() seems to be the easiest, it's not the fastest.

虽然end()看起来是最容易的,但它不是最快的。

The faster, and much stronger alternative is array_slice()

array_slice()是更快、更强的替代方法

Strike-out by editor: This claim has not been substantiated and contrary evidence has been mentioned in the comments. If there is any shred of truth to this poster's assertions about speed/efficiency then a benchmark test must be provided.

编辑:这一说法没有得到证实,而且在评论中也提到了相反的证据。如果这张海报关于速度/效率的断言有任何真相,那么就必须提供基准测试。

$last_key = key( array_slice( $array, -1, 1, TRUE ) );

#3


43  

I prefer

我更喜欢

end(array_keys($myarr))

#4


26  

Just use : echo $array[count($array) - 1];

使用:echo $array[count($array) - 1];

#5


19  

Dont know if this is going to be faster or not, but it seems easier to do it this way, and you avoid the error by not passing in a function to end()...

不知道这是否会更快,但这样做似乎更容易,并且避免了通过函数结束()来避免错误……

it just needed a variable... not a big deal to write one more line of code, then unset it if you needed to.

它只是需要一个变量…不需要再写一行代码,如果需要的话就取消设置。

$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

$keys = array_keys($array);
$last = end($keys);

#6


8  

Try using array_pop and array_keys function as follows:

尝试使用array_pop和array_keys函数如下:

<?php

$array = array(
    'one' => 1,
    'two' => 2,
    'three' => 3
);

echo array_pop(array_keys($array)); // prints three

?>

#7


3  

I would also like to offer an alternative solution to this problem.

我还想为这个问题提供另一种解决方案。

Assuming all your keys are numeric without any gaps, my preferred method is to count the array then minus 1 from that value (to account for the fact that array keys start at 0.

假设所有的键都是数字而没有任何间隔,我首选的方法是计算数组的值,然后从该值中减去1(来说明数组键从0开始的事实)。

$array = array(0=>'dog', 1=>'cat');

$lastKey = count($array)-1;
$lastKeyValue = $array[$lastKey];

var_dump($lastKey);
print_r($lastKeyValue);

This would give you:

这将给你:

int(1) cat

int(1)猫

#8


3  

You can use this:

您可以使用:

$array = array("one" => "apple", "two" => "orange", "three" => "pear");
end($array); 
echo key($array);

Another Solution is to create a function and use it:

另一个解决方案是创建一个函数并使用它:

function endKey($array){
end($array);
return key($array);
}

$array = array("one" => "apple", "two" => "orange", "three" => "pear");
echo endKey($array);

#9


3  

I just took the helper-function from Xander and improved it with the answers before:

我只是从Xander中提取了helper函数,并在之前对它进行了改进:

function last($array){
  $keys = array_keys($array);
  return end($keys);
}


$arr = array("one" => "apple", "two" => "orange", "three" => "pear");    
echo last($arr);
echo $arr(last($arr));

#10


2  

$arr = array('key1'=>'value1','key2'=>'value2','key3'=>'value3');
list($last_key) = each(array_reverse($arr));
print $last_key;
// key3

#11


-2  

$array = array(
    'something' => array(1,2,3),
    'somethingelse' => array(1,2,3,4)
);

$last_value = end($array);
$last_key = key($array); // 'somethingelse'

This works because PHP moves it's array pointer internally for $array

这是可行的,因为PHP将它的数组指针移动到$array内部。

#12


-3  

Like this:

是这样的:

$keys = array_keys($array);
$lastKey = $keys[sizeof($array)-1];

#13


-3  

It should be something like this...

应该是这样的……

$latest_kid = end(array_reverse(array_keys($my_array)));

Cause it reverse the array when we call array_keys.

因为当我们调用array_keys时它会反转数组。