# Can't figure out how to create hierarchical multi array in such situation

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I have the following multi array like this:
[0] => [ [0] => "A", [1] => "B", [2] => "C" ]
[1] => [ [0] => "A", [1] => "C", [2] => "D" ]
[2] => [ [0] => "B", [1] => "A", [2] => "B" ]
[3] => [ [0] => "B", [1] => "A", [2] => "D" ]

It should be transformed into this:
["A"] => [
["B"] => [
"C"
],
["C"] => [
"D"
]
],
["B"] => [
["A"] => [
"B", "D"
]
]

- that is to say, i need to split the above structure into classes,
subclasses, subsubclasses and so on, depth is variable...

I thought about recursive function, but can't figure out how it should
look like. Was fighting whole day over this task, but vain... :(

## Re: Can't figure out how to create hierarchical multi array in such situation

.oO(alexandis@gmail.com)

<?php
\$test = array(
array('A', 'B', 'C'),
array('A', 'C', 'D'),
array('B', 'A', 'B'),
array('B', 'A', 'D'),
);

\$result = array();
foreach (\$test as \$array) {
\$current = &\$result;
foreach (\$array as \$item) {
\$current = &\$current[\$item];
}
}

print_r(\$result);
?>

HTH
Micha

## Re: Can't figure out how to create hierarchical multi array in such situation

Strange, can't see my post.
First of all - thanks a lot, Micha.
I'm surprised, i haven't expected it was possible to create nested
array elements (subelements) implicitly, via reference :-o

\$current = &\$current[\$item];

I tried to do the same in recursive function...

## Re: Can't figure out how to create hierarchical multi array in such situation

.oO(alexandis@gmail.com)

If you want to reference a variable or an array element that doesn't
exist yet, it is created and set to NULL. There's a short note about
this in the manual:

What References Do
http://www.php.net/manual/en/language.references.whatdo.php

See the third note and the example on that page.

Micha

## Re: Can't figure out how to create hierarchical multi array in such situation

Thanks! One more question. Now i need to create selectlists filled
with data of every corresponding hierarchy class:

function nestedSelectionList(\$level, \$data, \$result, \$id = "") {
\$result[\$level] = "<select id=\"nest_".\$level."\" name=\"nest_".
\$level."\">";
foreach (\$data as \$key => \$value) {
\$id_key = (\$id == "" ? "" : \$id.":").\$key;
\$result[\$level] .= "<option value=\"".\$id_key."\">".\$key."</
option>";
if (is_array(\$value)) {
nestedSelectionList(\$level + 1, \$value, &\$result, \$id_key);
}
}
\$result[\$level] .= "</select>";
}

And first selectlist is created ok, but i can see that next ones -
not, because pointer is moved to the last element, so list 2, 3, ...
contain only 1 element :( I tried to play with reset(), but it didn't
help... How to manage this, please?

## Re: Can't figure out how to create hierarchical multi array in such situation

Ooops, seems like it has nothing to do with pointer, just inner
\$result[\$i] is being overwritten for every \$value item, so just the
last one remains.. :)