473,382 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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... :(
Nov 26 '07 #1
5 2100
..oO(al*******@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
Nov 26 '07 #2
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...
Nov 26 '07 #3
..oO(al*******@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
Nov 26 '07 #4
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?
Nov 27 '07 #5
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.. :)
Nov 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steve Jorgensen | last post by:
Hi all, I'm working on the schema for a database that must represent data about stock & bond funds over time. My connundrum is that, for any of several dimension fields, including the fund name...
3
by: Dominic Myers | last post by:
Hi there, this may be a bit out there but I have a XML data file of a hierarchy and I'd like to output the file using XSL, the thing is I don't know if it works like that? I've now got an got a...
2
by: Mriganka | last post by:
I want to create a hierarchical tree from the array of strings obtained by extracting a jar file.I want to do this using Javascript. For ex, I am having the entries in the array as...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
5
by: news | last post by:
Using php.net I got this far, and so far so good! But I can't fenagle it quite the way I need to. I'm creating an array (which I didn't know how to do before today, so a success for me *g*): ...
5
by: News VS.NET \( MS ILM \) | last post by:
hello, how do you declare a multi-dimentional constant array in C# does this make sense? Thanks
5
by: Richard Lewis Haggard | last post by:
I am trying to create multi-dimensioned arrays in conventional ASP pages and pass these arrays as arguments to functions that are in a C# interop assembly. ASP complains because it doesn't...
2
by: Chris | last post by:
I keep my data in an array and using TextMatrix property for filling the control cells. I am not using recordset. Is there any way for creating bands programmatically in a Hierarchical GridView...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.