php - Creating Arrays of Parents and Children -


i have array, , know desired output, can't quite head around how achieve it

the initial indexes "parent" id's, , arrays contained within children, children parents

my array: ```

array:10 [▼   0 => array:2 [▼     0 => 1     1 => 5   ]   1 => array:1 [▼     0 => 2   ]   2 => array:2 [▼     0 => 3     1 => 4   ]   5 => array:1 [▼     0 => 6   ]   6 => array:2 [▼     0 => 7     1 => 8   ]   9 => array:2 [▼     0 => 10     1 => 22   ]   10 => array:1 [▼     0 => 11   ]   11 => array:10 [▼     0 => 12     1 => 13     2 => 14     3 => 15     4 => 16     5 => 17     6 => 18     7 => 19     8 => 20     9 => 21   ]   22 => array:1 [▼     0 => 23   ]   23 => array:2 [▼     0 => 24     1 => 25   ] ] 

```

the resulting array: ```

0 9 0,1 0,5 0,1,2 0,1,2,3 0,1,2,4 0,5,6 0,5,6,7 0,5,6,8 9,10 9,22 9,10,11 9,10,11,12 9,10,11,13 9,10,11,14 9,10,11,15 9,10,11,16 9,10,11,17 9,10,11,18 9,10,11,19 9,10,11,20 9,10,11,21 9,22,23 9,22,23,24 9,22,23,25 

```

i'm thinking use recursion, can't life of me figure out how result

edit: further information:

if take:

  22 => array:1 [▼     0 => 23   ]   23 => array:2 [▼     0 => 24     1 => 25   ] 

23 parent of 24 , 25, , 22 parent of 23, therefore 22 grandparent of 24 , 25:

9,22,23,24 9,22,23,25 

that's how list (9 parent of 22, therefore grandparent of 23 , great grandparent of 24 , 25)

this bit advanced stuff can use iterators achieve this.

first, have extend recursiveiteratoriterator serve our need:

class flatrecursiveiteratoriterator extends recursiveiteratoriterator {     private $directory;      public function __construct(         traversable $iterator,         $mode = recursiveiteratoriterator::leaves_only,         $flags = 0,         array $directory     ) {         // set array children lookup.         $this->directory = $directory;         parent::__construct($iterator, $mode, $flags);     }      public function callhaschildren()     {         if ($this->getdepth() === 0 && is_array($this->current())) {             return true;         }          // see if children array availale in top array         // (lookup id).         return !empty($this->directory[$this->current()]) &&             is_array($this->directory[$this->current()]);     }      public function callgetchildren()     {         return new recursivearrayiterator(             $this->getdepth() === 0                  ? $this->current()                 : $this->directory[$this->current()]         );     } } 

having class use in combination recursivearrayiterator , recursivecallbackfilteriterator create needed $iterator:

$skip = []; $iterator = new flatrecursiveiteratoriterator(     // filter helps skip top level array elements     // if have ancestors. pay attencion $skip passed reference.     new recursivecallbackfilteriterator(         new recursivearrayiterator($array),         function ($current, $key, $_) use (&$skip) {             return !(is_array($current) && isset($skip[$key]));         }     ),     recursiveiteratoriterator::self_first,     0,     $array ); 

then can traverse $iterator pretty simple non-nested loop:

$stack = []; foreach ($iterator $node) {     $depth = $iterator->getdepth();         if ($depth > 0) {         $skip[$node] = true;     }      // use ternary operator top array has ids keys,     // inner arrays have ids values.     $stack[$depth] = $depth === 0         ? $iterator->key()         : $node;      // have slice out stack, may contain elements     // previous iterations.     echo implode(',', array_slice($stack, 0, $depth + 1)), php_eol; } 

here working demo.

this might lot of code, actually, not. , need take account of work done standard php library. have written case specific code.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -