array (
$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 />
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 />
配列 (
1 => 7,
7 => 11,
11 => 12,
12 => NULL 、
)