-2

I have a multidimensional array like below(figure 1). I just want to push all the sub array keys which are equal to parent array key as key values.

As an example lets say, parent key is 23. and 23 has a child key 24. child key 24 has sub child keys (25,26,27....) and also sub child key may have another child keys. So what I want is to push all the child, subchild, subsubchild... to parent key 23. See figure 2.

I really appreciate if someone can come up with a solution for this.

Thanks a lot..

Figure 1

Array ( [
  18] => Array ( 
    [1] =>
    [14] =>
  )
  [23] => Array ( 
    [24] =>
  ) 
  [24] => Array ( 
    [25] =>
    [26] =>
  )
 [26] => Array ( 
    [27] =>
    [28] =>
  )

)

Figure 2

Array ( [
  18] => Array ( 
    [1] =>
    [14] =>
  ) 
  [23] => Array ( 
    [24] => Array ( 
       [25] =>
       [26] => Array ( 
          [27] =>
          [28] =>
       )
     )
  )
)
7
  • Can you elaborate on why you want to do this? Commented Sep 6, 2013 at 5:00
  • 3
    Please format that array so it's readable. Commented Sep 6, 2013 at 5:02
  • So you want to flatten the keys of the array out into a single array? Commented Sep 6, 2013 at 5:13
  • Hello guys! @remyabel: these array keys are company id's I want to list all the sublisted companies under parent companies. Commented Sep 6, 2013 at 5:40
  • @keshu_vats: does this comment above gives u enough detail? Commented Sep 6, 2013 at 5:54

1 Answer 1

0

Heh luckily, I didn't have what to do whole day at school

I left there commented echos from my quick debugging

My Output:

Array 
(
    [18] => Array
        (
        [1] =>
        [14] =>
        ) 
    [23] => Array
        ( 
        [24] => Array
            ( 
            [25] =>
            [26] => Array
                ( 
                [27] =>
                [28] =>
                )
            )
        )
)

Code

<?php
function test ($scan, $where="array")
{
    global $array;
    global $position;
    //echo("\r\n New dimension: ".$where);

    foreach ($scan as $key => $value)
        {
            //echo("\r\n Scanning: ".$where."[".$key."]");
            if ( !isset($position[$key]) )
            {
                $position[$key] = $where."[".$key."]";
                //echo("\r\n Key position ".$key." set on: ".$position[$key]);
                //print("\r\n $position[$key]");
            }
            else
            {
                //echo("\r\n Key ".$key." is already set on position: ".$position[$key]);
                $old = ("\$".$where."[".$key."]");
                $cmd = ("\$".$position[$key]." = &".$old);
                //print("\r\n ".$cmd);
                eval("$cmd;");
                eval("unset ($old);");
            }
            if ( is_array($value) )
            {
                //echo("\r\n Enterring: ".$where."[".$key."]");
                test($value, $position[$key]);
                //echo("\r\n Enterring: ".$where);
            }
        }
    //echo("\r\n Exiting: ".$where);
}

print_r($array);
test($array);
print_r($array);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

You're a tunderbird. Awesome work mate. Thanks for you support. Appreciate it :)

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.