在数组中查找级别数量的算法


Algorithm to find amount of levels in an array of arrays

我正在研究一种算法来计算数组中的级别数量。

我需要这样做的原因是,我需要从属于类别父级的数据库中获取一个类别列表,并且根据该数组的级别数量,我需要显示一定数量的类别列表(以选择类别)。

因此,它将是每个类别级别的类别列表,例如

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

因此,我需要一个功能来检查所选父级的级别数量,例如,如果我通过了车辆ID,我希望它返回4或3,如果我们将车辆计数为0级,所以我知道如果客户从第一个列表中选择了维奇莱斯,我将不得不再显示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;
    }
}

我真的需要了解它是如何工作的,因为我必须处理几个像这样的函数,所以如果可以的话,请详细解释你的答案。

感谢

嵌套数组的深度等于其中最大数组的深度+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.

我对@GolezTrol在他们的回答中所说的话的解释("嵌套数组的深度等于其中最大数组的深度+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));
}

另一个具有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>';
}