Use list to process the tree data (the adjacent list). an existing array
$ D = array ('announcement ', 1, 0), array ('Article', 2, 0), array ('Article 1', 3, 2 ), array ('Article 2', 4, 2), array ('Article 1 comment', 5, 3), array ('Article 2 comment', 6, 4 ), array ('Article 1 comment 1', 7, 3), array ('Article 1 comment comment', 8, 5 ),);
Expected output:
Announcement article 1 Article 1 comment article 1 comment article 1 comment article 1 comment article 2 comment
So you can
foreach($d as $t) list($a[$pid][$id], $id, $pid) = $t;
Get
Array ([0] => Array ([1] => announcement [2] => article) [2] => Array ([3] => Article 1 [4] => Article 2) [3] => Array ([5] => Article 1 comment [7] => Article 1 comment 1) [4] => Array ([6] => Article 2 Comments) [5] => Array ([8] => Article 1 Comments ))
We can see that the data is clustered by 3rd columns.
So we can use a recursive function to display data.
function foo($ar, $pid=0, $deep=0) { foreach($ar[$pid] as $k=>$v) { printf("%s%s\n", str_repeat(' ', $deep), $v); if(isset($ar[$k])) foo($ar, $k, $deep+2); }}
Call foo ($ );
Reply to discussion (solution)
Moderator is a good guy
Bamboo has a special liking for an infinite number of trees.
Every time you read it, there are new gains.
Study in the front row!
I learned. Haha
It turns out to be like this.
It's really concise, learning.
Well written. I learned.
Static void Main (string [] args) {double a, B, c, p, h, area; Console. write ("Enter the triangle side A:"); string s = Console. readLine (); a = double. parse (s); Console. write ("Enter the triangle side B:"); s = Console. readLine (); B = double. parse (s); Console. write ("Enter the triangle side C:"); s = Console. readLine (); c = double. parse (s ); if (a> 0 & B> 0 & c> 0 & a + B> c & a + c> B & B + c> a) {Console. writeLine ("triangle three sides: a = {0}, B = {1}, c = {2} ", a, B, c); p = a + B + c; h = p/2; area = Math. sqrt (h * (h-a) * (h-B) * (h-c); Console. writeLine ("triangle perimeter = {0}, area = {1}", p, area);} else Console. writeLine ("cannot form a triangle! "); Console. ReadKey ();}
Sorry, the one above is wrong... I did not mean it .. I want to try out the editor function.
Learned
Niu X, learning
Http://www.javadad.com
Learned the moderator
Good, learning
Very good. I learned.
Support
Hao ......
Observation, learning, support, and scoring
I learned.
Thank you for sharing.
Thank you for sharing your learning!
Learned
Xuanjina
Simple and clear tree always uses recursion
Really good, very useful, can learn
The idea of LZ is the same as that used by me, but the code is much less than mine and much better than mine.
The idea of this algorithm is to create a second-level array with parent_id as the key name and call this array recursively.
Array (2978) {[0] => object (stdClass) #1 (4) {["id"] => string (8) "50094064" ["subject"] => string (22) "online video/e-book" ["parent_id"] => int (0) ["type_id"] => int (0 )}.........
The above is the organizational form. I am used to saving the generated objects of this classification to txt text. the generated custom key name cannot be directly list (). after obtaining the object using array_values, it is still abnormal to use list again, and it is abnormal to change the element key name of $ t again.
// The content in foreach is converted from the source array. $ t = array_values ($ t) cannot be used in the list form. list ($ id, $ a [$ pid] [$ t [$ id], $ pid, $ tid) = $ t;
So I changed it and used it again.
// Convert the source data to the object foreach ($ object_tmp as $ t) {$ t = array ($ t-> subject, $ t-> id, $ t-> parent_id ); list ($ a [$ pid] [$ id], $ id, $ pid) = $ t ;}
However, I changed the position.
// Convert the source data to the object foreach ($ object_tmp as $ t) {$ t = array ($ t-> id, $ t-> subject, $ t-> parent_id ); list ($ id, $ a [$ pid] [$ id], $ pid) = $ t ;}
I can't use it anymore. after reading the list explanation for a long time, I still cannot find the answer.
Attach my original practice:
For list to convert two-dimensional arrays, I personally adopt a clumsy method, the source array is similar to the above
// More one-dimensional (generally three-dimensional) arrays with the parent id as the key name $ tmp = array (); foreach ($ item_category as $ it) {if (count ($ tmp [$ it ["parent_id"]) {$ tmp [$ it ["parent_id"] [count ($ tmp [$ it ["parent_id"])] = $ it ;} else {$ tmp [$ it ["parent_id"] [0] = $ it ;}}
$ Tmp is an array equivalent to the LZ method $
I wrote it from the project file and wrote it in the controller. I won't change it.
// The Public variable var $ sorta = array () used to store data (sorted); // pid of the core part of tree sorting: public function get_all _ ($ pid, $ tmp) {$ tt = $ tmp [$ pid]; foreach ($ tt as $ ttt) {$ this-> sorta [count ($ this-> sorta)] = $ ttt; $ this-> get_all _ ($ ttt ["id"], $ tmp );}}
It's best to switch to javascript.
Learning, good
It's good. you must add it to your favorites.
It's easy and convenient.
The error is good. you must add it to favorites.
Easy to Learn
Easy to Learn
Learned
Read ..
I did this before, but I directly wrote recursive functions to retrieve them from the database.
Very? Big, and? To recruit
Observation, learning, support, and scoring