Can"t figure out how to create hierarchical multi array in such

Can"t figure out how to create hierarchical multi array in such

am 26.11.2007 01:13:30 von alexandis

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

am 26.11.2007 01:41:11 von Michael Fesser

..oO(alexandis@gmail.com)

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

$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

am 26.11.2007 13:30:48 von alexandis

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

am 26.11.2007 17:17:30 von Michael Fesser

..oO(alexandis@gmail.com)

>I'm surprised, i haven't expected it was possible to create nested
>array elements (subelements) implicitly, via reference :-o

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

am 27.11.2007 09:24:48 von alexandis

Thanks! One more question. Now i need to create selectlists filled
with data of every corresponding hierarchy class:
I've made recursive function

function nestedSelectionList($level, $data, $result, $id = "") {
$result[$level] = "";
}

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

am 27.11.2007 09:28:58 von alexandis

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