0

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
   )
)

2 Answers 2

2
$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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

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.