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... :( 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
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(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
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?
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.. :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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. ...
|
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...
|
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*):
...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
| |