Can't figure out how to create hierarchical multi array in suchsituation

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", [...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-26-2007
alexandis@gmail.com
 
Posts: n/a
Default Can't figure out how to create hierarchical multi array in suchsituation

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... :(
Reply With Quote
  #2 (permalink)  
Old 11-26-2007
Michael Fesser
 
Posts: n/a
Default Re: Can't figure out how to create hierarchical multi array in such situation

..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
Reply With Quote
  #3 (permalink)  
Old 11-26-2007
alexandis@gmail.com
 
Posts: n/a
Default Re: Can't figure out how to create hierarchical multi array in suchsituation

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...
Reply With Quote
  #4 (permalink)  
Old 11-26-2007
Michael Fesser
 
Posts: n/a
Default Re: Can't figure out how to create hierarchical multi array in such situation

..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
Reply With Quote
  #5 (permalink)  
Old 11-27-2007
alexandis@gmail.com
 
Posts: n/a
Default Re: Can't figure out how to create hierarchical multi array in suchsituation

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?
Reply With Quote
  #6 (permalink)  
Old 11-27-2007
alexandis@gmail.com
 
Posts: n/a
Default Re: Can't figure out how to create hierarchical multi array in suchsituation

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.. :)
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:16 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0