2011-02-04 92 views
1

我不是很強大的數組,但我需要確定如何計算子數組具有的父數,以便確定將其顯示爲SELECT中的選項的縮進值。PHP - 如何確定一個孩子數組的父母數量?

所以,如果我有這樣的數組:

array(
     'World'=>array(
      'North America'=>array(
        'Canada'=>array(
         'City'=>'Toronto' 
        ) 
      ) 
    ) 
); 

我怎麼會去決定很多家長「城市」如何在以翻譯成的空間我想作爲一個縮進的使用次數?

感謝您的任何幫助。

編輯:讓我們來看看,如果我能更好地解釋自己:

我有這樣的代碼我使用建立一個選擇的選項列表:

function toOptions($array) { 

    foreach ($array as $key=>$value) { 
     $html .= "<option value=\"" . $key . "\" >"; 

     $html .= $value['title']; 

     $html .= "</option>"; 

     if (array_key_exists('children', $value)) { 
      $html .= toOptions($value['children']); 
     } 

    } 

    return $html; 

} 

print toOptions($list); 

所以,我試圖確定如何獲得家長的數量,才能在這一行標題前加空格:

$html .= $value['title']; 

像:

$html .= "&nbsp;&nbsp;&nbsp;" . $value['title']; 

但是,我不知道如何弄清楚要添加多少空間。

希望這更清楚。

感謝您的幫助。

+0

你想知道這個數字只適用於`城市`或數組中的所有鍵? – 2011-02-04 02:25:07

+0

它取決於範圍..如果當前範圍是你在一些函數中只有數組('city'=>'Toronto'),那麼除非你知道有關數組的其他內容,像某些鍵等。 – arnorhs 2011-02-04 02:26:24

+0

你是如何來到「城市」而不知道你是如何到達那裏的? – Dolph 2011-02-04 02:26:24

回答

2
$x = array(
     'World'=>array(
      'North America'=>array(
        'Canada'=>array(
         'City'=>'Toronto' 
        ) 
      ) 
    ) 
); 

// This function do something with the key you've found in the array 
function visit($name, $depth) 
{ 
    echo $name . ' has ' . $depth . ' parents.'; 
} 

// This function visits all the contents aff $array 
function find_recursive($array, $depth = 0) 
{ 
    if (is_array($array)) { 
     foreach ($array as $k => $value) { 
      visit($k, $depth + 1); 
      find_recursive($array, $depth + 1); 
     } 
    } 
} 

申請探親:

find_recursive($x); 
1

好吧。關閉頂部你正在處理的是一個多維數組。

您可以在數組的每個級別上運行計數w/foreach,並使用foreach循環通過的每個級別返回的計數數+1。

我不確定這是否回答你的問題,但我試圖確切地知道你想要達到的目標。

1

正如你已經在使用遞歸函數來顯示數據,你可以擴展你的函數。有沒有需要更頻繁遍歷數組比一次:

function getWhitespaces($count) { 
    $result = ''; 
    while($count--) { 
     $result .= '$nbsp;'; 
    } 
    return $result; 
} 


function toOptions($array, $level=0) { 

    foreach ($array as $key=>$value) { 
     $html .= "<option value=\"" . $key . "\" >"; 

     $html .= getWhitespaces($level) + $value['title']; 

     $html .= "</option>"; 

     if (array_key_exists('children', $value)) { 
      $html .= toOptions($value['children'], $level + 1); 
     } 

    } 

    return $html; 

} 

print toOptions($list); 
0

請嘗試以下...您的解決方案的尖叫聲在我的腦海裏遞歸。它有點難看,但它似乎工作

$totraverse = array(
    'Moon' => array(
     'Dark Side' => "Death Valley" 
    ), 
    'Halley Commet' => "Solar System", 
    'World' => array(
     'North America' => array(
      'Canada' => array(
       'City' => 'Toronto' 
      ) 
     ), 'South America' => array(
      'Argentina' => array(
       'City' => 'Toronto' 
      ) 
     ) 
    ) 
); 


function traverse($totraverse_, $path="", $count=0) { 
    global $array; 
    // echo count($totraverse_) . " count\n"; 
    if (!is_array($totraverse_)) { 
     echo "returning $path and $key\n"; 
     return array($path, $count); 
    } else { 
     foreach ($totraverse_ as $key => $val) { 

      echo "assting $path and $key\n"; 
      $result = traverse($val, $path . "/" . $key, $count + 1); 
      if($result){ 
       $array[]=$result; 
      } 
     } 
    } 
    echo false; 
} 

$array = array(); 
traverse($totraverse); 

foreach($array as $item){ 
    echo "{$item[0]}--->{$item[1]}\n"; 
} 
相關問題