我有一個這樣的數組(存儲在$ optDir變量)創建選擇選項值列表:從一個數組
Array
(
[0] => Array
(
[id] => 8
[title] => Atasan Pria
[idparent] => 5
)
[1] => Array
(
[id] => 5
[title] => Fashion
[idparent] => 0
)
[2] => Array
(
[id] => 7
[title] => Motor
[idparent] => 0
)
[3] => Array
(
[id] => 6
[title] => Mobil
[idparent] => 0
)
[4] => Array
(
[id] => 9
[title] => Hem
[idparent] => 8
)
)
,我有PHP代碼是這樣的:
function optCatAds($pos=0, $level=0){
global $optDir; $opt = "";
$n = count($optDir);
for($i=0;$i<$n;$i++){
$l = $level*3;
$sign=""; for ($z=0;$z<=$l;$z++){$sign.=" ";}
if($optDir[$i]['idparent']==$pos){
$opt .= '<option value="'.$optDir[$i][id].'">'.$sign.$optDir[$i][title].'</option>';
optCatAds($optDir[$i][id], $level + 1);
}
}
return $opt;
}
當我打電話optCatAds功能,給出如下輸出:
<option value="5">Fashion</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>
不過,我想提出的輸出象下面這樣:
<option value="5">Fashion</option>
<option value="8"> Atasan Pria</option>
<option value="9"> Hem</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>
條件:
時尚,美孚,汽車< - 父
時尚有孩子Atasan Pria
Atasan PRIA有孩子下襬
有人能幫助我嗎?感謝您的幫助。
這個順序的依據是什麼??你可以手工做沒有循環 –
@Charafjra:請查看以上我的更新。謝謝 – Fredy