本帖最后由 xuzuning 于 2014-07-04 21:16:48 编辑
$a = array(<br /> 1 => 7,<br /> 7 => 11,<br /> 8 => 7,<br /> 9 => 8,<br /> 10 => 11,<br /> 11 => 12,<br /> 12 => NULL,<br />);<br />$i = 1;<br />while(isset($a[$i])) {<br /> $r[$i] = $a[$i]; <br /> $i = $a[$i];<br />}<br />$r[$i] = $a[$i]; <br />var_export($r);<br />
array (
1 => 7,
7 => 11,
11 => 12,
12 => NULL,
)
------解决方案--------------------<br />$map = array(<br /> 1 => 7,<br /> 7 => 11,<br /> 8 => 7,<br /> 9 => 8,<br /> 10 => 11,<br /> 11 => 12,<br /> 12 => NULL<br />);<br /><br />function getroad($map){<br /> $key = 1;<br /> $result = array();<br /> while($key!=NULL){<br /> $result[$key] = $map[$key];<br /> $key = $map[$key];<br /> }<br /> return $result;<br />}<br /><br />$result = getroad($map);<br /><br />var_export($result);<br />
array (
1 => 7,
7 => 11,
11 => 12,
12 => NULL,
)