This is a discussion on Can't figure out how to create hierarchical multi array in suchsituation within the PHP Language forums, part of the PHP Programming Forums category; I have the following multi array like this: [0] => [ [0] => "A", [1] => "B", [...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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... :( |
|
|||
|
..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... <?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 |
|
|||
|
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... |
|
|||
|
..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/languag...ces.whatdo.php See the third note and the example on that page. Micha |
|
|||
|
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] = "<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? |