473,770 Members | 4,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2121
..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 nestedSelection List($level, $data, $result, $id = "") {
$result[$level] = "<select id=\"nest_".$le vel."\" name=\"nest_".
$level."\">";
foreach ($data as $key =$value) {
$id_key = ($id == "" ? "" : $id.":").$key;
$result[$level] .= "<option value=\"".$id_k ey."\">".$key." </
option>";
if (is_array($valu e)) {
nestedSelection List($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
3732
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 itself, the dimension may be represented in different ways over time, and may split or combine from one period to the next. When querying from the database for an arbitrary time period, I need the data to be rolled up to the smallest extent...
3
2357
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 SVG file http://camshag.co.uk/svg/NHSTrust/index.html which reads the data and outputs the hierarchy as an image using a shed-load of ecmascript, this works but I'm wondering if there is a simpler way of doing it? I came across a script in the...
2
1767
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 com/asd/many/a.class com/asd/many/b.class com/asd/xyz/c.class com/asd/d.class com/asd/man/you/e.class
39
6553
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. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
3
2958
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 application that is going to be used to extract data from a legacy system (VSAM based mainframe file structure), and output data in pipe-delimited record layouts, multiple record types per file, one file per chosen client. I have been working on...
5
1990
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*): $a = array(); $a = "Name"; $a = $name; $a = "Message";
5
17236
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
2143
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 recognize that the created array is the same as the declared interface argument. The C# function is expecting to receive a 2 dimensioned array and is declared like this: public object InteropFunc( string s1, object arValues ); What should the ASP...
2
4669
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 Control for expanding and collapsing rows ? Thanks a lot in advance.
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.