2

I'm trying to arrange an array in levels. This is my array:

Array
  (
    [0] => Array(
      [0] => Array(
        [id] => 971249312[name] => Wolverine
      )

      [children] => Array(
        [0] => Array(
          [0] => Array(
            [id] => 735327624[name] => Ciclop
          )

          [children] => Array()
        )
      )
    )

    [1] => Array(
      [0] => Array(
        [id] => 1926833684[name] => Gambit
      )
      [children] => Array()
    )

    [2] => Array(
      [0] => Array(
        [id] => 51194629[name] => Quicksilver
      )
      [children] => Array()
    )
  )

See that the first position of array has 3 elements - this must be the level 0. The first position of these elements must be the level 1. The children of these elements are the next level and so on. I can´t figure out how to arrange it.

the expected output:

Array
(
    ["level_1"] => Array
        (
            [0] => Array
                (
                    [id] => 971249312
                    [name] => Wolverine
                )

            [1] => Array
                (
                    [id] => 1926833684
                    [name] => Gambit
                )

            [2] => Array
                (
                    [id] => 51194629
                    [name] => Quicksilver
                )

        )

    ["level_2"] => Array
        (
            [0] => Array
                (
                    [id] => 735327624
                    [name] => Ciclop
                )

        )

)
3
  • Can u figure out the desired output what you required Commented Feb 3, 2017 at 9:31
  • Please post your expected output Commented Feb 3, 2017 at 9:47
  • @KARTHISRV I edited the post with the expected output. Commented Feb 3, 2017 at 10:12

2 Answers 2

3

Another recursive tree walk.

I scan the tree 'depth first' so I need to keep track of the current 'level'.

Demonstration at eval.in

Tree scan routine:

/**
 * Recursive scan of the tree
 * 
 * @node   array    Current Node to be processed
 * @level  integer  Current depth of the tree
 * output  array    reference to where to store the details
 *   
 * @return void
 */   
function scanNode($node, $level, &$output)
{
    $outLevelIdx = 'level_'. $level; 

    foreach ($node as $idx => $info) {

        $parent = current($info); 

        $output[$outLevelIdx][] = array('id' => $parent['id'], 'name' => $parent['name']);

        if (!empty($info['children'])) { // go scan the children
            scanNode($info['children'], $level + 1, $output);
        }        
    }  
}  

Run the scan:

/*
 *  Output array in here - pass as a reference 
 */  
$output = array();

// scan the full tree
scanNode($source, 0, $output);

Sample output:

output
Array
(
    [level_0] => Array
        (
            [0] => Array
                (
                    [id] => 971249312
                    [name] => Wolverine
                )

            [1] => Array
                (
                    [id] => 1926833684
                    [name] => Gambit
                )

            [2] => Array
                (
                    [id] => 51194629
                    [name] => Quicksilver
                )
        )

    [level_1] => Array
        (
            [0] => Array
                (
                    [id] => 735327624
                    [name] => Ciclop
                )
        )
)
Sign up to request clarification or add additional context in comments.

Comments

2

If your desired output is

Array
(
    [0] => Array
        (
            [id] => 971249312
            [name] => Wolverine
            [children] => Array
                (
                )

        )

    [1] => Array
        (
            [id] => 971249312
            [name] => Wolverine
            [children] => Array
                (
                )

        )

    [2] => Array
        (
            [id] => 971249312
            [name] => Wolverine
            [children] => Array
                (
                )

        )

)

Then your code should be

$newArray = [];
foreach ($givenArray as $key => $value) {
    $newArray[$key]['id'] = $value[0]['id'];
    $newArray[$key]['name'] = $value[0]['name'];
    $newArray[$key]['children'] = $value['children'];
}

AS per your desired output

This function used to scan all the node and provide as per your requirment.

OUTPUT

$newArray = [];
myfunction($a, 0,$newArray);


function myfunction($loop, $level, &$newArray) {
    $index = "level_".$level;
    $i = 0;
    foreach ($loop as $key => $value) {
        foreach ($value as $key1 => $value1) {
            if($key1 !== 'children'){
                $newArray[$index][$i] = ['id' => $value1['id'], 'name' => $value1['name']];
                $i++;
            }
        }
        if (isset($value['children']) && !empty($value['children'])) { 
            myfunction($value['children'], $level + 1, $newArray);
        }
    }

}
print_r($newArray);exit;

1 Comment

@RyanVincent, KARTIS SRV thanks man you are great, work fine!

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.