如何从多个数组中获取最后一个数组?

时间:2011-12-01 18:14:47

标签: php arrays multidimensional-array

我有一个数组,其中包含一些其他数组:

如果我print_r($u)我得到了:

Array
(
    [0] => Albany
)
Array
(
    [0] => Albany
Array
(
    [0] => Albany
    [41] => Albuquerque
)
Array
(
    [0] => Albany
    [41] => Albuquerque
)
Array
(
    [0] => Albany
    [41] => Albuquerque
    [54] => Atlanta
)
Array
(
    [0] => Albany
    [41] => Albuquerque
    [54] => Atlanta
)
Array
(
    [0] => Albany
    [41] => Albuquerque
    [54] => Atlanta
    [93] => Auckland
    [94] => Augusta
)
Array
(
    [0] => Albany
    [41] => Albuquerque
    [54] => Atlanta
    [93] => Auckland
    [94] => Augusta
)

...

最后一个数组有大约20个元素。我只需要最后一个数组。希望这不会太混乱。

3 个答案:

答案 0 :(得分:5)

您可以使用end()功能:

$last_array = end($u);

请记住,调用end()将更改内部数组指针。如果您不希望这种情况发生,您可以这样做:

$last_array = $u[count($u) - 1]; // make sure count($u) > 0

答案 1 :(得分:0)

如果您想从数组$u中获取最后一项,可以使用end函数(无论其类型是什么)来执行此操作:

$lastarray = end($u);

答案 2 :(得分:0)

$arr = array_slice($u, -1);怎么样? (将返回仅包含所需数组的数组) 还是$arr = array_pop($u)? (也将其删除)