0

i have an array input like this ..

Array
(
    [0] => Array
        (
            [0] => 42
        )

    [**42**] => Array
        (
            [0] => 12
            [1] => 14
        )

    [**14**] => Array
        (
            [0] => 317
        )

    [317] => Array
        (
            [0] => 319
        )

    [**12**] => Array
        (
            [0] => 306
            [1] => 307
        )

    [307] => Array
        (
            [0] => 311
        )

    [306] => Array
        (
            [0] => 309
        )
)

and i want to get result array like bellow :

$paths[]=array(42,12,306,309);

$paths[]=array(42,12,307,311);

$paths[]=array(42,14,317,319);

see array input

  • root in array input = 42 (index of array 0)
  • 42 have child = 12, 14
  • 12 have child = 306, 307
  • 14 have child = 317
  • 306 have child = 309
  • 307 have child = 311
  • 317 have child = 319

like this..

and output array insert into $paths

$paths[0]=array(42,12,306,309);

$paths[1]=array(42,12,307,311);

$paths[2]=array(42,14,317,319);

5
  • What logic is array(42,12,306,309) based on? Commented Jan 17, 2011 at 7:36
  • It's hard to understand your question in the current form. Can you confirm the code sections are what you intended? Commented Jan 17, 2011 at 7:37
  • Could you try to explain a little more in detail what the problem or what you're trying to achieve? $paths[] pushes the other side of the equation into the array. I can't really follow the pattern you have in there atm.… Commented Jan 17, 2011 at 7:38
  • 2
    I think it's $a[0][0] == 42, $a[42][0] == 12, $a[12][0] == 306, $a[306][0] == 309, $a[309] doesn't exist. So, in short, the array represents a tree, and he wants $paths to be an array of all paths from the root to a leaf. Commented Jan 17, 2011 at 7:38
  • sorry if I can't explain properly.. wait few minutes i want to edit my question and coding.. thanks Commented Jan 17, 2011 at 7:42

2 Answers 2

2

This should do the trick:

function getpaths($arr, $node = 0){
    $path = array();
    foreach($arr[$node] as $next){
        if(isset($arr[$next])){
            $p = getpaths($arr, $next);
            foreach($p as $q){
                $path[] = array_merge(array($next), $q);
            }
        }else{
            $path[] = array($next);
        }
    }
    return $path;
}

Invoke as $path = getpaths($arr);

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

2 Comments

thanks Mark but, when i try this function got error "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes)". can you print your full coding and output..
@Kawah: sorry, made a typo when I added the default parameter; it's fixed now.
0

this is coding by Mark E, and i add one line coding to solve "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes)"

like this...

function getpaths($arr, $node = 0){
    $path = array();
    foreach($arr[$node] as $next){
        if(isset($arr[$next])){
            $node = $next;// adding by me
            $p = getpaths($arr, $node);
            foreach($p as $q){
                $path[] = array_merge(array($next), $q);
            }
        }else{
            $path[] = array($next);
        }
    }
    return $path;
}

thanks Mark E

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.