1

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.

1
  • 2
    It doesn't look like you have an multi-dimensional array ("array with other arrays inside it"). It looks like you are printing out the array $u while you loop though it and add elements. Commented Dec 1, 2011 at 18:17

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.