views:

140

answers:

2
Array
(
    [00000000017] => Array
        (
            [00000000018] => Array
                (
                    [00000000035] => I-0SAYHADW4JJA
                    [00000000038] => I-RF10EHE25KY0
                    [00000000039] => I-8MG3B1GT406F
                )

            [00000000019] => I-7GM4G5N3SDJL
        )

    [00000000025] => Array
        (
            [00000000011] => I-HT34P06WNMGJ
            [00000000029] => I-U5KKT1H8J39W
        )

    [00000000040] => I-GX43V2WP9KPD
    [00000000048] => I-XM526USFJAH9
    [00000000052] => I-M414RK3H987U
    [00000000055] => I-GABD4G13WHX7
)

I have the above array and i want to create a treeview display.. any recommendation ?

I guess i have to elaborate furthe on my question..

I want to store those array according to the level of array..

Example , I want something look like this :

[level_1]=> 00000000017,00000000025,00000000040, 00000000048, 00000000052
[level_2]=> 00000000018,00000000019, 00000000011, 00000000029
[level_3]=> 00000000035, 00000000038, 00000000039

A: 

You want a modified breadth-first search. This has the correct results for your sample structure:

<?php

function BFTraverse(&$tree = NULL, $depth = 0)
{
    if (empty($tree))
      return FALSE;

    $keys = array_keys($tree);
    $struct["lvl_$depth"] = $keys;

    foreach ($keys as $key)
    {
        if (is_array($tree[$key]))
        {
            $struct = array_merge_recursive($struct, BFTraverse($tree[$key], $depth + 1));
        }
    }

    return $struct;
}

$data = array
('00000000017' => array
        (
            '00000000018' => array
                (
                    '00000000035' => 'I-0SAYHADW4JJA',
                    '00000000038' => 'I-RF10EHE25KY0',
                    '00000000039' => 'I-8MG3B1GT406F'
                ),

            '00000000019' => 'I-7GM4G5N3SDJL'
        ),

    '00000000025' => array
        (
            '00000000011' => 'I-HT34P06WNMGJ',
            '00000000029' => 'I-U5KKT1H8J39W'
        ),

    '00000000040' => 'I-GX43V2WP9KPD',
    '00000000048' => 'I-XM526USFJAH9',
    '00000000052' => 'I-M414RK3H987U',
    '00000000055' => 'I-GABD4G13WHX7'
 );

$var = BFTraverse($data);
$i = 0;

foreach ($var as $level)
    echo "Level " . ++$i . ': ' . implode(', ', $level) . "\n";
?>

The output is:

Level 1: 00000000017, 00000000025, 00000000040, 00000000048, 00000000052, 00000000055
Level 2: 00000000018, 00000000019, 00000000011, 00000000029
Level 3: 00000000035, 00000000038, 00000000039

edit: Modified in the sense that you want the keys and not the node values.

jasonbar
cool jasonbar... it is exactly what i want .you are a star.... but just want to correct a minor mistake on the function name BFTraverse inside the function itself... suppose to be BFTraverse instead of BFTtraverse .. :-)
redcoder
A: 

I ran into the same problem recently and this article by Kevin van Zonneveld helped me out.

Basically you have to use a recursive function. Check out the article, it's what you need!

Alex