php - How can I build an associative array recursively using keys from another array? -
ok, bit tricky , hit wall.
what have array of grouping options:
array ( [0] => array ( [9] => sum [10] => sum [11] => avg ) [4] => array ( [9] => sum [10] => sum [11] => avg ) [9] => array ( [9] => sum [10] => sum [11] => avg ) )
what trying array looks this:
array ( [0] => array ( [4] => array ( [9] => array ( [9] => sum [10] => sum [11] => avg ) ) ) )
i example shows 3 levels deep need dynamic there different grouping options available. 1 level easy there 2 or more also. nested levels should have same keys parent levels.
what doing using iterate on data. keys indexes in array of data grouping. issue is, need lower levels of grouping aware of other indexes necessary proper grouping of multiple datasets. try simplify processing large datasets. need calculate lowest level (in example 9) can go on final array calculate grouped data each parent grouping. i'll use key 9 data build key 4 data , key 4 data build key 0 data. reason keys need aware of parents data unique parent group. $a[0][4][9] not same $a[0], $a[4], $a[9]. need 9 know it's dependent on 4 , 4 dependent on 0.
i'm not sure if php can or if makes sense. can dynamically build array that?
this recursive 'tree', except 'one-way' rather 'multi-way'. i.e. 'linked list'. 'node data' stored in final node identical in each node.
as usual: working code @ codepad.org
i found quite 'interesting' work out should 'initialize' output , 'recurse' with. anyway here code...
i decided simplify code dealing 'special cases' explicitly @ start...
if (count($source) <= 0) { return array(); } elseif (count($source) == 1) { return array(key($source) => current($source)); }
next, function work...
/** * add nodes 'tree' * * @param type $outnode -- 'reference' current node added * @param type $curkey -- index of current node -- need when overwriting 'null' value later. * @param type $source -- reference source array not want copies made * * @return array -- current node */ function addnode(&$outnode, $curkey, &$source) { // current node details... $curkey = key($source); $curitems = current($source); // advance next source node... next($source); // final node in list? if (current($source) !== false) { // more nodes add. need recurse... $nextkey = key($source); $outnode[$curkey] = array($nextkey => null); return addnode($outnode[$curkey], $nextkey, $source); // recurse } // add items last node $outnode[$curkey] = $curitems; return $outnode; }
the starting conditions , build output...
// generated tree in here $outtree = array(); // use key(), current() , next() functions use 'internal' array iterator. $curkey = key($source); $curitems = current($source); $outtree[$curkey] = null; // 'null' gets overwritten later. // build tree... $lastnode = addnode($outtree, $curkey, $source); // show output... echo '<pre>'; print_r($outtree); echo '</pre>'; exit;
output using supplied data:
array ( [0] => array ( [4] => array ( [9] => array ( [9] => sum [10] => sum [11] => avg ) ) ) )
Comments
Post a Comment