Use list to process tree data (adjacent list)

Source: Internet
Author: User
A list is used to process tree data (the adjacent list). an existing array $ d & nbsp ;=& nbsp; array (& nbsp; 'ads ', & nbsp; 1, & nbsp; 0 & nbsp;), & nbsp; array)
An existing array
$ D = array (
Array ('bulletins ', 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 Comments ', 8, 5 ),
);
Expected output:
Announcement
Article
Article 1
Article 1 comment
Article 1 comment
Article 1 comment 1
Article 2
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 comments
[7] => Article 1 comment 1
)

[4] => Array
(
[6] => Article 2 comment
)

[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 ($ );
Share The php list tree:
------ Solution --------------------
Moderator is a good guy
------ Solution --------------------

Bamboo has a special liking for an infinite number of trees.
Every time you read it, there are new gains.
------ Solution --------------------

------ Solution --------------------
Study in the front row!
------ Solution --------------------
I learned. Haha

------ Solution --------------------

------ Solution --------------------
It turns out to be like this.
------ Solution --------------------
It's really concise, learning.
------ Solution --------------------
Well written. I learned.
------ Solution --------------------
Static void Main (string [] args)
{
Double a, B, c, p, h, area;
Console. Write ("Enter the triangle side :");
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>)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.