Patrioticcow
Patrioticcow

Reputation: 27058

How to get the last array from multiple arrays?

I have an array that has some other arrays inside it:

if I print_r($u) I get:

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
)

...

The last array has about 20 elements. I need only that last array. Hope this is not too confusing.

Upvotes: 1

Views: 195

Answers (3)

djb
djb

Reputation: 6011

How about $arr = array_slice($u, -1);? (will return an array containing only your desired array) Or $arr = array_pop($u)? (removes it as well)

Upvotes: 0

Jon
Jon

Reputation: 437574

If you want to just get the last item out of an array $u, you can do it with the end function (regardless of what its type is):

$lastarray = end($u);

Upvotes: 0

user142162
user142162

Reputation:

You can use the end() function:

$last_array = end($u);

Keep in mind that calling end() will change the internal array pointer. If you don't want that to happen, you could do:

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

Upvotes: 5

Related Questions