2015-02-07 53 views
1

我正在研究算法以計算數組數組中的級別數量。查找數組中的級別數量的算法

我需要這個的原因是因爲我需要從屬於父類別的數據庫中獲取類別列表,並且根據此數組所具有的級別數量,我需要顯示一定數量的類別列表(選擇類別)。

因此,這將是對於類別中的各電平的類別列表,例如

Vehicles 
     Cars 
      honda 
        Red 
        Blue 
        Yellow 
      ford 
        Red 
      suzuki 
        Red 
        Green 
      BMW 
     Motorcycles 
      bla bla 
       bla bla 
Groceries 
     Fruits 
      Berries 
        Red 
         Strawberries 

所以我需要一個函數來檢查所選父的水平的量,例如,如果我通過的ID如果我們將車輛計爲0級,我希望它返回4或3,所以我知道如果客戶從第一個列表中選擇了Vechicles,我將不得不顯示3個列表。

到目前爲止,我有什麼不工作是

function count_children_level($list_of_children, $start_depth = 0){  

    // if the data being passed is an array 
    if(is_array($list_of_children)){ 

     // amount of nodes is equal to the 
     $max = $start_depth; 

     foreach($list_of_children as $i){ 

      $result = count_children_level($i, $start_depth + 1); 

      if ($result > $max){ 

       $max = $result; 
      } 
     } 
     return $max; 
    } 
    //if is not array 
    else { 
     return $start_depth; 
    } 
} 

我真的需要理解它是如何工作的,因爲我有幾個功能,這樣的工作之一,所以請你,說明你的詳細回答。

感謝

回答

2

嵌套數組的深度等於最大陣列的所以你的遞歸函數的這+ 1

,不是每,可以時間的推移,整個陣列的深度進行實際的遞歸調用,只獲取子數組的深度。所以這個函數對於一個普通的扁平數組返回1,對於每個級別都返回1。

<?php 
function array_depth($array) { 
    // Determine largest sub-array. Start with 0 if there are no arrays at all. 
    $max = 0; 
    foreach ($array as $item) { 
    if (is_array($item)) { 
     // Make the recursive call, passing not $array, but the sub-array ($item) 
     // to the function again. 
     $depth = array_depth($item); 
     if ($depth > $max) 
     $max = $depth; 
    } 
    } 
    // Depth of this array is the depth of the largest sub-array + 1. 
    return $max + 1; 
} 

我把它叫做是這樣的:

echo array_depth(
    array('x' => 
    array('y' => 
     array('z')))); // Returns 3. 
+0

嗨GolezTrol,非常感謝你的回答,你能不能請稍微解釋一下? – octohedron 2015-02-07 14:00:24

+0

你可以發佈你使用的數組並將其添加到你的問題?我會檢查我的代碼,並在需要的地方修復它,或者解釋發生了什麼問題。 – GolezTrol 2015-02-07 14:13:09

+0

你是對的,它按預期工作,但我傳遞的數組可能只有兩個級別,事情是我不需要知道數組的深度,但是類別的深度,因爲從數據庫中獲取的是什麼1級別的數組,具有在category_id和category_parent中指定的類別結構,所以它會比我想深入的要複雜得多 – octohedron 2015-02-07 14:15:39

2

我什麼@GolezTrol在their answer說解釋(「嵌套數組的深度等於其最大陣列的深度+ 1"):

function array_depth($a) 
{ 
    // If $a is not an array or it's an empty array then its depth is 1 
    if (! is_array($a) || count($a) == 0) { 
     return 0; 
    } 

    // Otherwise, add 1 to the maximum depth of the elements it contains 
    return 1 + max(array_map('array_depth', $a)); 
} 
+0

它應該'返回0;'當一個項目不是一個數組。但除此之外,這段代碼工作正常。 :)檢查空陣列是一個有趣的補充。它取決於你的定義,你是否想將一個空數組作爲一個額外的級別。 – GolezTrol 2015-02-07 14:22:05

0

RecursiveIteratorIterator類的另一種解決方案。這樣,你並不需要一個遞歸函數:

$array = array(
    'Vehicles' => array(
     'Cars' => array(
      'honda' => array(
       'Red', 
       'Blue', 
       'Yellow', 
      ) 
     ) 
    ) 
); 

function getTotalDepth($array) { 
    $iterator = new RecursiveIteratorIterator(
     new RecursiveArrayIterator($array) 
    ); 
    $max = 0; 
    foreach ($iterator as $element) { 
     if (!$iterator->callHasChildren()) { 
      $max = max($max, $iterator->getDepth()); 
     } 
    } 
    return $max; 
} 

echo getTotalDepth($array); 

此外,如果你想要遍歷整個數組非常有用:

$iterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($array), 
    RecursiveIteratorIterator::SELF_FIRST 
); 

foreach ($iterator as $element) { 
    print_r($element); 
    echo '<br>'; 
} 
相關問題