Using list to process tree data (adjacency list)
An existing array
$d = Array (
Array (' Announcements ', 1, 0),
Array (' article ', 2, 0),
Array (' Article 1 ', 3, 2),
Array (' Article 2 ', 4, 2),
Array (' Article 1 comments ', 5, 3),
Array (' Article 2 Comments ', 6, 4),
Array (' Article 1 reviews 1 ', 7, 3),
Array (' Article 1 comment comments ', 8, 5),
);
Expect the following output
Notice
Article
Article 1
Article 1 comments
Article 1 Comment comments
Posts 1 Comments 1
Article 2
Article 2 Comments
So you can
foreach ($d as $t) list ($a [$pid] [$id], $id, $pid) = $t;
Get
Array
(
[0] = = Array
(
[1] = = Announcement
[2] = = Articles
)
[2] = = Array
(
[3] = = Article 1
[4] = = Article 2
)
[3] = = Array
(
[5] = + Article 1 comments
[7] = + Article 1 comments 1
)
[4] = = Array
(
[6] = + Article 2 comments
)
[5] = = Array
(
[8] = + Article 1 Comment Comment
)
)
As you can see, the data is clustered in column 3rd.
Then a recursive function can be used to achieve the presentation of the 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 ($a);
------Solution--------------------
Moderators are a great guy.
------Solution--------------------
The owner of the infinite-level tree has a unique feeling.
Every time I see a new harvest.
------Solution--------------------
------Solution--------------------
Front Row Study!
------Solution--------------------
Study. Oh
------Solution--------------------
------Solution--------------------
That's how it turns out.
------Solution--------------------
Really concise, study.
------Solution--------------------
Good writing, study
------Solution--------------------
static void Main (string[] args)
{
Double A, B, C, p, h, area;
Console.Write ("Please enter the edge of the Triangle A:");
String s = Console.ReadLine ();
A = double. Parse (s);
Console.Write ("Please enter the edge of the Triangle B:");
S = console.readline ();
B = Double. Parse (s);
Console.Write ("Please enter the edge of the Triangle 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 (three sides of the triangle are: a={0},b={1},c={2} ", A, B, c);
p = A + B + C;