2013-12-17 35 views
1

我有一個這樣的數組(存儲在$ 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.="&nbsp;";} 
      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> 

條件:

  1. 時尚,美孚,汽車< - 父

  2. 時尚有孩子Atasan Pria

  3. Atasan PRIA有孩子下襬

有人能幫助我嗎?感謝您的幫助。

+0

這個順序的依據是什麼??你可以手工做沒有循環 –

+0

@Charafjra:請查看以上我的更新。謝謝 – Fredy

回答

1

這將正常工作爲你

$optDir = Array(Array("id"=>8,"title"=>"Atasan Pria","idparent"=>5), 
       Array("id"=>5,"title"=>"Fashion","idparent"=>0), 
       Array("id"=>7,"title"=>"Motor","idparent"=>0), 
       Array("id"=>6,"title"=>"Mobil","idparent"=>0), 
       Array("id"=>9,"title"=>"Hem","idparent"=>8) 
       ); 

function optCatAds($pos=0, $level=0) 
{     
    global $optDir;$opt; 
    for($i=0;$i<count($optDir);$i++) 
    {   
     $l = $level*3; 
     $sign=""; 
     for ($z=0;$z<$l;$z++){$sign.="&nbsp;";} 
     if($optDir[$i]['idparent']==$pos) 
     { 
      $opt .= '<option value="'.$optDir[$i]['id'].'">'.$sign.$optDir[$i]['title'].'</option>'; 
      $opt .= optCatAds($optDir[$i]['id'], $level + 1); 
     } 
    }        
    return $opt; 
} 

$res = optCatAds($pos=0, $level=0); 
echo "<select>{$res}</select>"; 
+0

我剛剛添加了你的關卡空間:) – Dwza

+0

好的,條件...生病了,給我5分鐘 – Dwza

+0

你只需要forgott添加$ opt。=到你的recursiv函數行12,當然還有一些'in your代碼 – Dwza

0

你可以嘗試foreach代替for循環,

foreach($optDir as $k=>$value){  
    $opt .= '<option value="'.$value->id.'">'.$sign.$value->title.'</option>';  
} 
+0

我想要命令遵循親子結構 – Fredy

0

更換$optDir[$i][title]$optDir[$i]['title']

$optDir[$i][id]$optDir[$i]['id']

在這裏,你正在遞歸調用等你拿無法完成

function optCatAds($pos=0, $level=0){     
     global $optDir; $opt = ""; 
     foreach($optDir as $each){ 
      if($each['id']==8) $level = 2; 
      if($each['id']==9) $level = 3; 
      $l = $level*3; 
      $sign=""; for($z=0;$z<=$l;$z++){$sign.="&nbsp;";} 
      if($each['idparent']==$pos){ 
       $opt .= '<option value="'.$each['id'].'">'.$sign.$each['title'].'</option>'; 
      } 
     }        
     return $opt; 
    } 
+0

的結果保持不變。 '<期權價值= 「5」>時尚 <期權價值= 「6」>美孚 <期權價值= 「7」>電機' – Fredy

+0

@Fredy檢查我的編輯 – Parixit

+0

我認爲這是不那麼有效,如何如果有很多ID? – Fredy

0
<?php 

$optDir = Array(
    Array(
     'id' => 8, 
     'title' => 'Atasan Pria', 
     'idparent' => 5 
    ), 
    Array(
     'id' => 5, 
     'title' => 'Fashion', 
     'idparent' => 0 
    ), 
    Array(
     'id' => 7, 
     'title' => 'Motor', 
     'idparent' => 0 
    ), 
    Array(
     'id' => 6, 
     'title' => 'Mobil', 
     'idparent' => 0 
    ), 
    Array(
     'id' => 9, 
     'title' => 'Hem', 
     'idparent' => 8 
    ) 
); 

function getOptionsRecursive($idParent = 0, $spaces = '') { 
    global $optDir; 
    $s = ''; 
    for($i = 0 ; $i < sizeof($optDir) ; $i++) { 
     $current = $optDir[$i]; 
     if($current['idparent'] == $idParent) { 
      $s .= '<option value="'.$current['id'].'">'.$spaces.$current['title'].'</option>'; 
      $s .= getOptionsRecursive($current['id'], $spaces.' '); 
     } 
    } 
    return $s; 
}; 

echo getOptionsRecursive(); 

?>