Justin
Justin

Reputation: 305

manipulating nested multidimensional array

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 )

Upvotes: 3

Views: 781

Answers (1)

Adam Wagner
Adam Wagner

Reputation: 16107

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.

Upvotes: 2

Related Questions