<html>
	<head>
		<title>Sort and indent</title>
	</head>
	<body>
		<?php
			$myArray = array(
				"1"  => "0", 
				"2" => "1", 
				"8" => "1",
				"3" => "2",
				"7" => "2",
				"4" => "3",
				"5" => "3",
				"6" => "3",
				"9" => "8",
				"10" => "9"
			);

			# Get the number of indention levels needed, given a key into $myArray. 
			function get_indent($key) 
			{
				global $myArray;

				if ($myArray[$key] == "0") {
					return 0;
				} else {
					return get_indent($myArray[$key]) + 1;
				}
			}
		
			# Print assoc array before sorting and indenting.
			echo "Before:<br>";
			while(list($key, $value) = each($myArray)) {
				echo "$key / $value<br>";	
			}

			# Sort the array.
			ksort($myArray, SORT_NUMERIC);
			reset($myArray);

			# Determine indention level and print with indent.
			echo "<br>After:<br>";
			while(list($key, $value) = each($myArray)) {
				echo str_repeat("--", get_indent($key)),"$key / $value<br>";
			}
		?>
	</body>
</html>

