I have multidimensional (tree) array of form like this: -
$a = array
-
(
-
"t" => array("b")
-
"d" => array(
-
"p" => array("b")
-
"n" => array(
-
"p" => array ( "b")
-
)
-
)
-
"p" => array("b")
-
"n" => array(
-
"p" => array("b")
-
)
-
)
-
That needs converting into 2 dimensional (tree respecting) array like this: -
$a = array
-
(
-
array("t", "b"),
-
array("d", "p", "b"),
-
array("d", "n", "p", "b"),
-
array("p", "b"),
-
array("n", "p", "b")
-
)
-
Any ideas!? Solve this and beer and pizza is on me :)
Looks like a depth-first search. - {
-
current node = root
-
current path = nothing
-
PrintPath(node, path)
-
}
-
-
PrintPath(node, path){
-
if no children -> print path and return
-
foreach childNode on current node{
-
PrintPath(childNode, path + childnode)
-
}
-
}
path would be a dynamically expanding array. path is passed by value, not reference, so depending on your language, you may have to copy the array.
6 3324
Hm maybe this should be moved to algorithms
I'll move it to algorithms. :)
Looks like a depth-first search. - {
-
current node = root
-
current path = nothing
-
PrintPath(node, path)
-
}
-
-
PrintPath(node, path){
-
if no children -> print path and return
-
foreach childNode on current node{
-
PrintPath(childNode, path + childnode)
-
}
-
}
path would be a dynamically expanding array. path is passed by value, not reference, so depending on your language, you may have to copy the array.
Language is php. I came up with this:
@jkmyoung
Thanks to your post I came up with much neater solution. -
printPath($a);
-
-
function printPath($node, $path = array()){
-
if(!is_array($node)){
-
$path[] = $node;
-
print_r($path);
-
return;
-
}
-
-
foreach($node as $key => $child_node){
-
$p = $path;
-
if(is_array($child_node)){
-
$p[] = $key;
-
printPath($child_node, $p);
-
}else{
-
printPath($child_node, $p);
-
}
-
}
-
}
-
Thanx. Beer and pizza is on me if we ever meet :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: me |
last post by:
I've posted this in the microsoft news group but just noticed the comp
newsgroups. What's the difference anyways?
This one is a tricky one so I'm interested in seeing what all you gurus have
to...
|
by: googleo |
last post by:
Hi,
in my application I want to handle and store data in a hierarchic data
structure.
For example: persons who manage houses; houses have various numbers of
floors; floors have various numbers...
|
by: Dadi |
last post by:
Hi,
I can make a simple initialization work like this:
Object ONE_ROW = {{"Vodafone", "5550160100197016"}};
But, now I want to create another array that consists of multiple copies of...
|
by: Yanhao Zhu |
last post by:
Hi, all,
If I have an array like
int m = new int { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items
in...
|
by: Toxick |
last post by:
Hello experts,
I'm a total C# noob with a total C# noob question.
I've been Serializing in C++ (MFC) and writing data to std::fstreams for
quite some time, so maybe I'm not understanding...
|
by: leonardo.calado |
last post by:
Hi,
I have a simple problem, but I don't found a solution for my problem.
I'm try, try and not found. If anybody help me, I very, very thankful
for this help.
Here is my problem:
I have a...
|
by: rflloyd |
last post by:
I wish to create a property of a control which is an array of Images, such that I can add, edit and delete images in the VS2005 property window. I've created the property as:
private Image...
|
by: Kailash Nadh |
last post by:
Hello all.
I have this tab formatted hierarchical structure.
--------------------
0 A
1 B
2 C
2 D
1 E
--------------------
|
by: BravoFoxtrot |
last post by:
Hi,
I'm trying to build an index into a multi dimensional associative
array. I may not know how many dimensions there are so i want to pass
the array indexes as a variable.
$arrayToAccess =...
|
by: zr |
last post by:
Hi,
Is there a way to initialize a std::tr1::array with a pre-allocated
built-in array in a copy-less assignment, such that both will point to
the same memory?
Vice-versa is easy to do, simply...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
| |