1

So, array_reverse() is supposed to take an array, and return a copy of it with elements in reverse order, and it's always been working faithfully for me, up until today. I have an array of arrays, and I need to get them in reverse order, so I'd assumed array_reverse would do exactly what I needed, but it seems to be behaving oddly, and I can't figure out why.

My initial array (yes, I'm doing some HTML parsing):

Array(
    0 => Array(
        0 => '<blockquote',
        1 => int 232
    ),
    1 => Array(
        0 => '<blockquote',
        1 => int 593
    ),
    2 => Array(
        0 => '<blockquote',
        1 => int 938
    ),
    3 => Array(
        0 => '</blockquote',
        1 => int 2627
    ),
    4 => Array(
        0 => '</blockquote',
        1 => int 2758
    ),
    5 => Array(
        0 => '</blockquote',
        1 => int 2794
    ),
)

running array_reverse($arr) on it, one would expect the returned array to be:

Array (
    0 => Array(
        0 => '</blockquote',
        1 => int 2794
    ),
    1 => Array(
        0 => '</blockquote',
        1 => int 2758
    ),
    2 => Array(
        0 => '</blockquote',
        1 => int 2627
    ),
    3 => Array(
        0 => '<blockquote',
        1 => int 938
    ),
    4 => Array(
        0 => '<blockquote',
        1 => int 593
    ),
    5 => Array(
        0 => '<blockquote',
        1 => int 232
    ),
)

However, it returns an unaltered array. Now, if I do array_reverse($arr[0]), then it reverses the entire array like I want.

I can think of no reason it would not be reversing the initial array, and why, when passing a specific key, it would reverse the entire array, rather than that one sub-array. Why would it be doing this?

EDIT: I walked away and came back, looked at it, and realized the main array had a single element which was the array I'd posted above. Just a good ol' ID-10-T error.

4
  • Can you create an example test case (e.g. on codepad) which shows the behaviour? Commented Apr 5, 2012 at 17:03
  • 1
    Works for me: codepad.org/68nDM11F Commented Apr 5, 2012 at 17:03
  • 1
    "yes, I'm doing some HTML parsing" Nice fending of the shit storm ;) Commented Apr 5, 2012 at 17:04
  • 1
    I walked away from it for a few minutes and came back, and realized I'm an idiot. I somehow completely missed seeing the extra array level at the beginning, so now it makes sense. Commented Apr 5, 2012 at 17:09

1 Answer 1

1

However, it returns an unaltered array.

It should just work.

Now, if I do array_reverse($arr[0]), then it reverses the entire array like I want.

Are you sure you don't have an extra level in your array?? I'm pretty sure you have an extra level!

Sign up to request clarification or add additional context in comments.

1 Comment

Yup! Just looking at it more closely (stepped away for a few minutes came back) and indeed I do! I don't know how I missed seeing this, and I feel like an idiot.

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.