views:

68

answers:

2

Just need some help taking this hierarchical array...

    Array (
       [root] => Array ([attr] => Array ([id] => 1) [label] => Array ([value] => My Root)
          [node] => Array (
             [0] => Array ([attr] => Array([id] => 2) [label] => Array([value] => Category 1)
                [node] => Array(
                   [0] => Array ([attr] => Array ([id] => 14) [label] => Array ([value] => Sub-Category 1))
                   [1] => Array([attr] => Array ([id] => 15) [label] => Array([value] => Sub-Category2))
etc, etc,

...and modifying it to match this array format

Array (
   [Category 1] => Array(
      [14] => Sub-Category 1
      [15] => Sub-Category 2
   )
)
A: 

You can use Foreach to look at every node array.

You should create a recursive function that return the final array and look in the closest level node.

Natim
+2  A: 
$newArray = array();
foreach ($array['root']['node'] as $node)
{
  $newArray[ $node['value'] ] = array();

  foreach ($node['node'] as $nestedNode)
    $newArray[ $node['value'] ][ $nestedNode['attr']['id'] ] = $nestedNode['label']['value'];
}

$newArray - is your result!

Tyler
Thank you very much!
EddyR