3
Array
(

    [178] => Array
        (
        )

    [179] => Array
        (
            [180] => 
            [181] => 
            [182] => 
            [183] => 
        )

    [184] => Array
        (
            [185] => 
        )

    [186] => Array
        (
        )

    [189] => Array
        (
            [190] => 
        )

    [181] => Array
        (
            [191] => 
            [192] => 
        )

    [192] => Array
        (
            [194] => 
        )

)

I have a 'linked list' and this PHP array is a list of all the nodes. I have used keys to store unique mysql ID for later lookup. You'll notice that some keys in the 2nd level arrays are the same as first level. I would like to join those arrays so that a lower level gets joined to an upper level in recursive fashion.

eg, 179 -> 181 -> 192 -> 194

There may be many node levels, not just what I have in this example. How do I recursively add all the nodes together into the correct order?

UPDATED I also have an array of all the ends on the node, ie the IDs that have no further nodes.

Array ( [0] => 178 [1] => 180 [2] => 182 [3] => 183 [4] => 185 [5] => 186 [6] => 190 [7] => 191 [8] => 194 )

5
  • 1
    i suspect a better db query will retrieve the data you want with out having to redo it in php Commented Nov 14, 2011 at 2:52
  • You can use a recursive function for that task. Commented Nov 14, 2011 at 2:58
  • which nodes in the second level are the same as the first level? Commented Nov 14, 2011 at 3:08
  • what is on the end of the node ? to be sure that is end ? can we be sure... Commented Nov 14, 2011 at 3:20
  • @bensiu I have updated the question to show an array with all the node ends. Homer6 I think i'd need a function to search for which nodes have the same IDs and then join them accordingly Commented Nov 14, 2011 at 3:41

1 Answer 1

2

I'm not positive this is what you are looking for, and I'm sure there are plenty more efficient ways to do this. but here's a shot at it:

Given the input example you mentioned above.

This code:

function index_nodes($nodes, &$index) {
    foreach($nodes as $key => $value) {
        if ($value) {
            $index[$key] = $value;
            index_nodes($value, $index);
        }
    }
}

function nest_list($list) {
    $index = array();
    index_nodes($list, $index);

    // Construct tree
    $build_tree = function(&$value, $key) use ($index, &$updated) {
        if(array_key_exists($key, $index)) {
            $value = $index[$key];
            $updated = true;
        }
    };

    // This needs done several times, since I can't be sure I nested things
    // in the perfect order.
    do {
        $updated = false;
        array_walk_recursive($list, $build_tree);
    } while($updated);

    return $list;
}

Run like so:

$list2 = nest_list($list);
print_r($list2);

Gives the following output:

Array
(
    [178] => 
    [179] => Array
        (
            [180] => 
            [181] => Array
                (
                    [191] => 
                    [192] => Array
                        (
                            [194] => 
                        )
                )
            [182] => 
            [183] => 
        )
    [184] => Array
        (
            [185] => 
        )
    [186] => 
    [189] => Array
        (
            [190] => 
        )
    [181] => Array
        (
            [191] => 
            [192] => Array
                (
                    [194] => 
                )

        )
    [192] => Array
        (
            [194] => 
        )
)

Once again... big pile of code, but I think it gets you closer to your goal.

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

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.