2

I have an array like a one way list:

array(
    'data1' => <some data>,
    'next' => array(
        'data2' => <some data>,
        'next' => array(
            'data3' => <some data>,
            'next' => array(
                'data4' => <some data>,
                'next' => array(
                    'data5' => <some data>,
                    'next' => ..... etc to data n
                );
            );
        );
    );
);

I need to get data from inside arrays in reverse order. (data n, ... , data 2, data 1) Do You know any nice method for this?

5
  • 2
    Give example of desired output ... reverse can be sort , flip or backtrack Commented Oct 22, 2012 at 8:16
  • i need just get data from this array structure in revers: "data n" -> "data n-1" -> .... -> "data1" Commented Oct 22, 2012 at 8:21
  • It sill not clear ... With the array example above edit your qurstion and add the final desired output with all the keys you used ,data1,next,data2,next etc Commented Oct 22, 2012 at 8:23
  • Add your expected output, this will resolve the confusion. Commented Oct 22, 2012 at 8:24
  • pls look at this array. its like a tree with one branch. i want to get data from the last leafe and keep going to the root Commented Oct 22, 2012 at 8:29

5 Answers 5

3

You're not looking for the reverse of the array, but you're looking for something reverse.

Getting a better understanding first of the kind of reverse might help you.

You need the parent of each individual element. The parent is always the previous one if you traverse on next. So if you add the previous one as the parent, then the last element of the array is the one you're looking for.

So sounds straight forward. The harder part is to express this with so called variable aliasing / references.

Let's add all the parents and while traversing the array, removing the 'next' entry after referencing it:

/* traverse the path on 'next' and keep previous to set 'parent' of current */
$walk = &$array; // start at root node
while ($walk) {

    if (isset($previous)) {
        $walk['parent'] = &$previous;
    }

    $previous = &$walk;

    $hasNext = array_key_exists('next', $walk);
    if ($hasNext) {
        $walk = &$walk['next'];
        unset($previous['next']);
    } else {
        break;
    }
}
unset($previous);

As written the last element then would contain the array you're looking for. That last element is $walk here:

print_r($walk);

Which gives you (Demo):

Array
(
    [data5] => <some data5>
    [parent] => Array
        (
            [data4] => <some data4>
            [parent] => Array
                (
                    [data3] => <some data3>
                    [parent] => Array
                        (
                            [data2] => <some data2>
                            [parent] => Array
                                (
                                    [data1] => <some data1>
                                )
    ...
)

Hope this is helpful and understandable.

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

2 Comments

You save a lot of my time @hakre! this is what I need! Thank You very very much!
@JohnGrey: Your question was not that clear in the first place. Didn't see the edit when I first commented. Take care to ask concrete questions and make clear what your problem is for your next questions. A good question already contains it's answer ;)
0

Use the native reverse functions, or:

$myArray[count($myArray) - ++$index]

Comments

0

how about this?

http://php.net/manual/en/function.array-walk.php

http://php.net/manual/en/function.array-reverse.php

If you can tell us what you want to do, it will be easier for us to help you.

EDIT: From your comment I believe you are lookin' for the array_reverse function.

Here is what you're lookin for:

PHP Need to recursively reverse an array

Comments

0

please notice that this will not reverse the inside array.

$arr = array(
    'data1' => 'some data',
    'next' => array(
        'data2' => 'some data',
        'next' => '..... etc to data n'
    )
);
$rev = array_reverse($arr);
print_r($rev);

Comments

-3

use array_reverse(Array); function it may help for solving question.

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.